On 11/11/2022 2:22 AM, DFS wrote:

[(0,11), (1,1),  (2,1),
  (0,1) , (1,41), (2,2),
  (0,9) , (1,3),  (2,12)]

The set of values in elements[0] is {0,1,2}

I want the set of max values in elements[1]: {11,41,12}

This request is ambiguous. Do you want to get the maximum value for each row, and present it in the order of those rows? Your data list does not distinguish the rows from the tuples, so that information must come from somewhere else. Could it sometimes be different from 3? How are we supposed to know?

Also, as has already been noted in this thread, the result cannot literally be a set because sets are not ordered but you insisted that the order is important.

This code allows for different length of rows, and tries to be as clear as possible:

data = [(0,11), (1,1),  (2,1),
 (0,1), (1,41), (2,2),
 (0,9), (1,3),  (2,12)]

span = 3  # Change for other row lengths

d1 = [y for x, y in data]  # pick out the 2nd member of each tuple
# d1: [11, 1, 1, 1, 41, 2, 9, 3, 12]

groups = []
for i in range(0, len(d1), span):
    group = []
    for s in range(span):
        group.append(d1[i + s])
    groups.append(group)
# groups: [[11, 1, 1], [1, 41, 2], [9, 3, 12]]

maxes = [max(t) for t in groups]
# maxes: [11, 41, 12]

This could be condensed, but I recommend keeping it as clear as possible. Tricky, condensed code becomes harder to understand as time goes by.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to