Compare list item and return list

I have a list of lists, I don’t know the length of the main list, but each "sublist" necessarily contains 6 floats. I need to compare each float of each sublist and keep the smaller for the first three floats and the higher for the last three, and finally, I need to return all these values ​​to a list with 6 floats in the same order.

Here is an example:

list1 = [[-2.0, 0.0, -2.0, 2.0, 10.0, 2.0], [-1.0, 0.0, 2.0, 1.0, 5.0, 4.0]]
# Compare list1[0][0] with list1[1][0]
# Will return -2.0 (because the index is between 0 and 2 so it returns the lower float)
# Compare list1[0][4] with list1[1][4]
# Will return 10.0 (because the index is between 3 and 5 so it returns the higher float)
# The final result which should be returned is:
# [-2.0, 0.0, -2.0, 2.0, 10.0, 4.0]

list2 = [[-2.0, 0.0, -2.0, 2.0, 10.0, 2.0], [-1.0, 0.0, 2.0, 1.0, 5.0, 4.0], [3.0, 0.0, -1.0, 4.0, 1.0, 0.0]]
# Compare list2[0][2] with list2[1][2] with list2[2][2]
# Will return -2.0 (because the index is between 0 and 2 so it returns the lower float)
# The final result which should be returned is:
# [-2.0, 0.0, -2.0, 4.0, 10.0, 4.0]

I read about zip(), sets, understanding lists and different topics on this site, but I could not achieve what I wanted.

+4
source share
3 answers

zip(*list2), , .. , 3 3.

zipped = zip(*list2)
result = [min(zipped[i]) for i in range(3)] + [max(zipped[i]) for i in range(3, 6)]

Python 3 zip() , , Python 2 . range() . Python 2, itertools.

import itertools

zipped = itertools.izip(*list2)
result = [min(zipped.next()) for _ in range(3)] + [max(zipped.next()) for _ in range(3)]

: , zip() .

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> zip(*a) # you need `list(zip(*a))` in Python 3
[(1, 4), (2, 5), (3, 6)]

, zip(*[[1, 2, 3], [4, 5, 6]]) zip([1, 2, 3], [4, 5, 6]), , .

+6

"", 2

list1 = [[-2.0, 0.0, -2.0, 2.0, 10.0, 2.0], [-1.0, 0.0, 2.0, 1.0, 5.0, 4.0]]
output = [0 for x in range(6)]
# iterate over the 6 numbers
for i in range(6):
    value = list1[0][i] # pick the first number
    #iterate over all the lists, if we find a bigger/smaller one our current one then save it
    for j in range(len(list1)):
        if i >= 3 and list1[j][i] > value:
                value = list1[j][i]
        elif i < 3 and list1[j][i] < value:
                value = list1[j][i]
    #put the biggest/smallest value in the correct place in the output array
    output[i] = value
print output
+1

NumPy

NumPy:

np.concatenate((np.min(a[:,:3], axis=0), np.max(a[:,3:], axis=0)))

import numpy as np

list1 = [[-2.0, 0.0, -2.0, 2.0, 10.0, 2.0], [-1.0, 0.0, 2.0, 1.0, 5.0, 4.0]]
list2 = [[-2.0, 0.0, -2.0, 2.0, 10.0, 2.0], [-1.0, 0.0, 2.0, 1.0, 5.0, 4.0],
         [3.0, 0.0, -1.0, 4.0, 1.0, 0.0]]

a1 = np.array(list1)
a2 = np.array(list2)

for a in [a1, a2]:
    print(list(np.concatenate((np.min(a[:,:3], axis=0), np.max(a[:,3:], axis=0)))))

[-2.0, 0.0, -2.0, 2.0, 10.0, 4.0]
[-2.0, 0.0, -2.0, 4.0, 10.0, 4.0]
0

Source: https://habr.com/ru/post/1626015/


All Articles