I encountered a quite odd problem while dealing with the max of a long list of lists of of pairs, such as
[
[(0, 1), (1, 1), (2, 1), (3, 4), (4, 1), (5, 1), (6, 1),...,(141,3)],
...,
[(12, 1), (36, 1), (91, 1), (92, 1), (110, 1),..., (180, 1)]
]
I am trying to get the maximum of the first element of all the pairs. Pythonically, I was doing:
max([max(x) for x in list])[0]
which actually returns the correct number, IF the list is shorter than 281 lists. In fact, as soon as the list is longer than 280, I get this message
ValueError: max() arg is an empty sequence
So, for a long list
max([max(x) for x in list[0:280]])[0]
it's fine, while
max([max(x) for x in list[0:281]])[0]
breaks.
Am I doing something wrong here?
You have an empty list among your list of lists, at index 280. Slicing up to [:280]
excludes it, and it is included with [:281]
.
This is easily reproduced with a shorter sample:
>>> lsts = [
... [(0, 1), (1, 1)],
... [(2, 1), (3, 4)],
... [(4, 1), (5, 1)],
... [], # empty at index 3
... ]
>>> max(max(x) for x in lsts)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <genexpr>
ValueError: max() arg is an empty sequence
>>> max(max(x) for x in lsts[:3]) # include everything before index 3
(5, 1)
You can avoid the issue altogether by chaining your lists together, here using chain.from_iterable()
:
from itertools import chain
max(chain.from_iterable(list_of_lists))[0]
This treats all nested lists as one long list, an empty list somewhere in between simply doesn't contribute to that new sequence.
Why not just this?
max([max([t[0] for t in sl if t]) for sl in l if sl])
You can extract the first item from the beginning. Empty lists and tuples are ignored.
EDIT
max([max([t for t in sl if t]) for sl in l if sl])[0]
is more efficient.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.