0

I have two list of list with integers:

List1 = [[1],[2],[3]]
List2 = [[4],[5],[6],[7]]

and let's assume one list from List1 is extracted and one list from List2 is extracted and both of these goes through a defined multiplication function and values appended into a list.

Example would be: [1] from List1 and [4] from List2 goes through the function to obtain the value 4. This would apply to [1] from List1 and [5] from List2 and so on until all combinations are fulfilled.

the output list would be:

[(4,[1],[4]),(5,[1],[5]),(6,[1],[6]),(7,[1],[7]),(8,[2],[4]),(10,[2],[5]),(12,[2],[6]),(14,[2],[7]),(12,[3],[4]),(15,[3],[5]),(18,[3],[6]),(21,[3],[7])]

In case you are wondering, i added the [] containing the number from each list in the output just for a better perspective on how the output is obtain, 1x4 =4 and 1x5 etc..

What i want now would be instead of giving me all combinations in a list, i want to only output the smallest value from each list element. Example, out of 1x4,1x5,1x6,1x7, 1x4 gives the smallest value therefore, only the value for 1x4 is output. moving on, 2x4,2x5,2x6,2x7, 2x4 gives the smallest value therefore, 2x4 is the output. the same goes for the rest.

I have the code where it outputs the list with the entire combination but I'm not sure how would i extract only the smallest value from all the combinations.

results = []
for i in L1:
    for j in L2:
        multiply = multiplication(i,j)
        results.append(multiply)
print(results)

Any help is appreciated.

Electric
  • 517
  • 11
  • 25

3 Answers3

0

You can use reduce to figure up a value in an iterable object:

results = [(4,[1],[4]),(5,[1],[5]),(6,[1],[6]),(7,[1],[7]),
           (8,[2],[4]),(10,[2],[5]),(12,[2],[6]),(14,[2],[7]),
           (12,[3],[4]),(15,[3],[5]),(18,[3],[6]),(21,[3],[7])]
reduce( ( lambda x,y: x if x[0]<y[0] else y), results )
dani herrera
  • 48,760
  • 8
  • 117
  • 177
0

In the case, where both lists are just one-dimensional lists containing integers, you could do it the simple way.

results = []
for i in L1:
    minimum = i*L2[0]
    for j in range(1, len(L2)):
        if i*L2[j] < minimum:
            minimum = i*L2[j]
    results.append(minimum)
print(results)
0

Since you want to get minimum just by the first element in the tuple, you cound use min to get minimum through all products with i from L1 in the loop.

Here is a complete example.

L1 = [[1],[2],[3]]
L2 = [[4],[5],[6],[7]]

def multiplication(i,j):
    return (i[0]*j[0], i, j)

results = []
for i in L1:
    results_for_i = []
    for j in L2:
        multiply = multiplication(i,j)
        results_for_i.append(multiply)
    results.append(min(results_for_i))
print(results)

Otherwise for computing min you would need su supply a key function mapping the tuple to key by which you want to compute the minimum.

For details about this aproach see the excelent answer for python max function using 'key' and lambda expression

Community
  • 1
  • 1
matusko
  • 3,487
  • 3
  • 20
  • 31