I have this data:
self.data = [(1, 1, 5.0),
(1, 2, 3.0),
(1, 3, 4.0),
(2, 1, 4.0),
(2, 2, 2.0)]
When I run this code:
for mid, group in itertools.groupby(self.data, key=operator.itemgetter(0)):
for list(group)i get:
[(1, 1, 5.0),
(1, 2, 3.0),
(1, 3, 4.0)]
what i want
But if I use 1 instead of 0
for mid, group in itertools.groupby(self.data, key=operator.itemgetter(1)):
To group by the second number in tuples, I get only:
[(1, 1, 5.0)]
even if there are other tuples that have a “1” in that 1 (2nd) position.