Length of generator output [duplicate]
问题 This question already has answers here : Getting number of elements in an iterator in Python (16 answers) What's the shortest way to count the number of items in a generator/iterator? (6 answers) Closed last year . Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn\'t find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like: def iterlen(x): n = 0 try: while