Not sure how to describe the issue in exact sense...
Is trying to create a function that returns multiple filtered element lists:
def search_test():
find = [('1', '2'), ('3', '4')]
abc = [1,6,15]
result = []
for i in abc:
for j in range(len(find)): # <- How to loop through the individual element list?
if float(find[j][0]) * float(find[j][1]) <= i:
result.append(i)
return(result)
print(search_test())
Basically, the function expect to calculate the product of values from find
then compare it with list abc
. If the product is greater than elements in abc
then append to a new list called result
currently the result I get is [6,15,15]
however the expecting the output to be [[6,15],[15]]
. On the other words is that the function currently considering values in find
as whole instead of two independent lists of their own element values.
The idea of the function is supposed to be:
So are there any ways to access the individual element lists to perform further calculation(s) then append to a new list as multiple sub-lists?
CodePudding user response:
You have not clearly mentioned whats need to be achieved here but as per my understanding you interchanged the for loops. It seems you want to know what elements of abc satisfy the condition for each tuple in find. So first for loop should be on find After that you need to create result list for each element in find and append that list to final_result
def search_test():
find = [('1', '2'), ('3', '4')]
abc = [1, 6, 15]
final_result = []
for j in range(len(find)):
result = []
for i in abc:
if float(find[j][0]) * float(find[j][1]) <= i:
result.append(i)
final_result.append(result)
return (final_result)