Length of generator output [duplicate]
The easiest way is probably just sum(1 for _ in gen) where gen is your generator.
The easiest way is probably just sum(1 for _ in gen) where gen is your generator.
The conversion to list that’s been suggested in the other answers is the best way if you still want to process the generator elements afterwards, but has one flaw: It uses O(n) memory. You can count the elements in a generator without using that much memory with: sum(1 for x in generator) Of course, be … Read more
One google behind this stackoverflow result, I found that there is a numpy.fromiter(data, dtype, count). The default count=-1 takes all elements from the iterable. It requires a dtype to be set explicitly. In my case, this worked: numpy.fromiter(something.generate(from_this_input), float)
Generators can’t be rewound. You have the following options: Run the generator function again, restarting the generation: y = FunctionWithYield() for x in y: print(x) y = FunctionWithYield() for x in y: print(x) Store the generator results in a data structure on memory or disk which you can iterate over again: y = list(FunctionWithYield()) for … Read more
You can use GeneratorType from types: >>> import types >>> types.GeneratorType <class ‘generator’> >>> gen = (i for i in range(10)) >>> isinstance(gen, types.GeneratorType) True
Suggestion: def peek(iterable): try: first = next(iterable) except StopIteration: return None return first, itertools.chain([first], iterable) Usage: res = peek(mysequence) if res is None: # sequence is empty. Do stuff. else: first, mysequence = res # Do something with first, maybe? # Then iterate over the sequence: for element in mysequence: # etc.
It’s used to send values into a generator that just yielded. Here is an artificial (non-useful) explanatory example: >>> def double_inputs(): … while True: … x = yield … yield x * 2 … >>> gen = double_inputs() >>> next(gen) # run up to the first yield >>> gen.send(10) # goes into ‘x’ variable 20 … Read more
Generators give you lazy evaluation. You use them by iterating over them, either explicitly with ‘for’ or implicitly by passing it to any function or construct that iterates. You can think of generators as returning multiple items, as if they return a list, but instead of returning them all at once they return them one-by-one, … Read more
Using the ES2015 Spread operator: […Array(n)].map() const res = […Array(10)].map((_, i) => { return i * 10; }); // as a one liner const res = […Array(10)].map((_, i) => i * 10); Or if you don’t need the result: […Array(10)].forEach((_, i) => { console.log(i); }); // as a one liner […Array(10)].forEach((_, i) => console.log(i)); Or … Read more
Note: this post assumes Python 3.x syntax.† A generator is simply a function which returns an object on which you can call next, such that for every call it returns some value, until it raises a StopIteration exception, signaling that all values have been generated. Such an object is called an iterator. Normal functions return … Read more