generator

How do I check if an iterator is actually an iterator container?

徘徊边缘 提交于 2019-12-21 12:33:12
问题 I have a dummy example of an iterator container below (the real one reads a file too large to fit in memory): class DummyIterator: def __init__(self, max_value): self.max_value = max_value def __iter__(self): for i in range(self.max_value): yield i def regular_dummy_iterator(max_value): for i in range(max_value): yield i This allows me to iterate over the value more than once so that I can implement something like this: def normalise(data): total = sum(i for i in data) for val in data: yield

Generator functions in express with bluebird and co

喜夏-厌秋 提交于 2019-12-21 09:39:10
问题 I'm trying out some of the harmony features in node 0.12, in particular trying out the new generators feature. I'm doing this with co (v4), bluebird and express (v4), something like this: // ... var fs = bluebird.promisifyAll(require('fs')); // ... app.post('/test', co.wrap(function* (req, res, next) { var contents = yield fs.readFileAsync('/etc/hosts', 'utf8'); return res.send(contents); })); // ... According to its documentation, co.wrap returns a normal function that returns a promise from

How to unzip an iterator?

倾然丶 夕夏残阳落幕 提交于 2019-12-21 08:03:40
问题 Given a list of pairs xys , the Python idiom to unzip it into two lists is: xs, ys = zip(*xys) If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory? 回答1: Suppose you have some iterable of pairs: a = zip(range(10), range(10)) If I'm correctly interpreting what you are asking for, you could generate independent iterators for the firsts and seconds using itertools.tee: xs, ys = itertools.tee(a) xs, ys = (x[0] for x in xs), (y[1] for y in ys) Note

How to unzip an iterator?

拟墨画扇 提交于 2019-12-21 08:01:07
问题 Given a list of pairs xys , the Python idiom to unzip it into two lists is: xs, ys = zip(*xys) If xys is an iterator, how can I unzip it into two iterators, without storing everything in memory? 回答1: Suppose you have some iterable of pairs: a = zip(range(10), range(10)) If I'm correctly interpreting what you are asking for, you could generate independent iterators for the firsts and seconds using itertools.tee: xs, ys = itertools.tee(a) xs, ys = (x[0] for x in xs), (y[1] for y in ys) Note

Can yield produce multiple consecutive generators?

谁说我不能喝 提交于 2019-12-21 07:21:28
问题 Here are two functions that split iterable items to sub-lists. I believe that this type of task is programmed many times. I use them to parse log files that consist of repr lines like ('result', 'case', 123, 4.56) and ('dump', ..) and so on. I would like to change these so that they will yield iterators rather than lists. Because the list may grow pretty large, but I may be able to decide to take it or skip it based on first few items. Also, if the iter version is available I would like to

Can yield produce multiple consecutive generators?

半腔热情 提交于 2019-12-21 07:21:25
问题 Here are two functions that split iterable items to sub-lists. I believe that this type of task is programmed many times. I use them to parse log files that consist of repr lines like ('result', 'case', 123, 4.56) and ('dump', ..) and so on. I would like to change these so that they will yield iterators rather than lists. Because the list may grow pretty large, but I may be able to decide to take it or skip it based on first few items. Also, if the iter version is available I would like to

Generate from generators

帅比萌擦擦* 提交于 2019-12-21 06:07:42
问题 I have a generator that takes a number as an argument and yields other numbers. I want to use the numbers yielded by this generator and pass them as arguments to the same generator, creating a chain of some length. For example, mygenerator(2) yields 5, 4 and 6. Apply mygenerator to each of these numbers, over and over again to the numbers yielded. The generator always yields bigger numbers than the one passed as argument, and for 2 different numbers will never yield the same number.

Random number generator that returns only one number each time

孤街浪徒 提交于 2019-12-21 05:54:07
问题 Does Python have a random number generator that returns only one random integer number each time when next() function is called? Numbers should not repeat and the generator should return random integers in the interval [1, 1 000 000] that are unique. I need to generate more than million different numbers and that sounds as if it is very memory consuming in case all the number are generated at same time and stored in a list. 回答1: You are looking for a linear congruential generator with a full

compute mean in python for a generator

十年热恋 提交于 2019-12-21 03:39:28
问题 I'm doing some statistics work, I have a (large) collection of random numbers to compute the mean of, I'd like to work with generators, because I just need to compute the mean, so I don't need to store the numbers. The problem is that numpy.mean breaks if you pass it a generator. I can write a simple function to do what I want, but I'm wondering if there's a proper, built-in way to do this? It would be nice if I could say "sum(values)/len(values)", but len doesn't work for genetators, and sum

Why is the range object “not an iterator”? [duplicate]

妖精的绣舞 提交于 2019-12-20 17:27:24
问题 This question already has an answer here : If range() is a generator in Python 3.3, why can I not call next() on a range? (1 answer) Closed 5 years ago . I wrote this and expected 0 : >>> x = range(20) >>> next(x) Instead I got: TypeError: 'range' object is not an iterator But I thought it was a generator? The initial answer yielded the same thing I initially said to myself: it's an iterable, not an interator. But then, that wouldn't explain why this works, if both are simply generators: >>>