generator

Is there a way to remember the position in a python iterator?

给你一囗甜甜゛ 提交于 2019-12-22 04:41:07
问题 I would like to iterate over an iterable object (let's say, a list) and leave at some point remembering the position where I left off to continue the next time an iterator for that object is called. Something like: for val in list: do_stuff(val) if some_condition: break do_stuff() for val in list: continue_doing_stuff(val) Speed matters and the list considered is quite large. So saving the object and iterating again through the whole list until the saved element is found is not an option. Is

PHP - json_encode a generator object (using yield)

一曲冷凌霜 提交于 2019-12-22 03:42:13
问题 I have a very large array in PHP (5.6), generated dynamically, which I want to convert to JSON. The problem is that the array is too large that it doesn't fit in memory - I get a fatal error when I try to process it (exhausted memory). So I figured out that, using generators, the memory problem will disappear. This is the code I've tried so far (this reduced example obvisously doesn't produce the memory error): <?php function arrayGenerator()// new way using generators { for ($i = 0; $i < 100

PHP - json_encode a generator object (using yield)

淺唱寂寞╮ 提交于 2019-12-22 03:41:44
问题 I have a very large array in PHP (5.6), generated dynamically, which I want to convert to JSON. The problem is that the array is too large that it doesn't fit in memory - I get a fatal error when I try to process it (exhausted memory). So I figured out that, using generators, the memory problem will disappear. This is the code I've tried so far (this reduced example obvisously doesn't produce the memory error): <?php function arrayGenerator()// new way using generators { for ($i = 0; $i < 100

TypeError: can't pickle generator objects

删除回忆录丶 提交于 2019-12-22 03:21:43
问题 I am trying to write some result on to pickle file as below: raw_X = (self.token_ques(text) for text in training_data) with open('/root/Desktop/classifier_result.pkl', 'wb') as handle: pickle.dump(raw_X, handle) Error: raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle generator objects Any help would be much appreciable. 回答1: Don't use a generator expression when you want to pickle data. Use a list comprehension instead, or call list() on the generator to

Efficiently generate all composite numbers less than N (with their factorizations)

帅比萌擦擦* 提交于 2019-12-22 02:33:08
问题 I'd like to build an efficient Python iterator/generator that yields: All composite numbers less than N Along with their prime factorization I'll call it "composites_with_factors()" Assume we already have a list of primes less than N, or a primes generator that can do the same. Note that I: DO NOT need the numbers to be yielded in numerical order DO NOT care if 1 is yielded at the beginning or not DO NOT care if primes are yielded, too I figure this can be done with a clever recursive

How to print the content of the generator?

本小妞迷上赌 提交于 2019-12-21 23:13:59
问题 N = [1, 2, 3] print(n for n in N) results: <generator object <genexpr> at 0x000000000108E780> why this code couldn't print: 1 2 3 However the code: sum(n for n in N) could sum up all the number in N. Could you please tell me why sum() could work but print() faild? 回答1: It's because you passed a generator to a function and that's what __repr__ method of this generator returns. If you want to print what it would generate, you can use: print(*N, sep='\n') # * will unpack the list or print(*(n

Audio generation software or .NET library

被刻印的时光 ゝ 提交于 2019-12-21 18:29:48
问题 I need to be able to play certain tones in a c# application. I don't care if it generates them on the fly or if it plays them from a file, but I just need SOME way to generate tones that have not only variable volume and frequency, but variable timbre. It would be especially helpful if whatever I used to generate these tones would have many timbre pre-sets, and it would be even more awesome if these timbres didn't all sound midi-ish (meaning some of them sounded like the might have been

Audio generation software or .NET library

那年仲夏 提交于 2019-12-21 18:29:33
问题 I need to be able to play certain tones in a c# application. I don't care if it generates them on the fly or if it plays them from a file, but I just need SOME way to generate tones that have not only variable volume and frequency, but variable timbre. It would be especially helpful if whatever I used to generate these tones would have many timbre pre-sets, and it would be even more awesome if these timbres didn't all sound midi-ish (meaning some of them sounded like the might have been

In-place dictionary inversion in Python

可紊 提交于 2019-12-21 17:56:54
问题 I need to invert a dictionary of lists, I don't know how to explain it in English exactly, so here is some code that does what I want. It just takes too much memory. def invert(oldDict): invertedDict = {} for key,valuelist in oldDict.iteritems(): for value in valuelist: try: entry = invertedDict[value] if key not in entry: entry.append(key) except KeyError: invertedDict[value] = [key] return invertedDict The original is a dict of lists, and the result is a dict of lists. This "inverts" it.

In-place dictionary inversion in Python

跟風遠走 提交于 2019-12-21 17:56:29
问题 I need to invert a dictionary of lists, I don't know how to explain it in English exactly, so here is some code that does what I want. It just takes too much memory. def invert(oldDict): invertedDict = {} for key,valuelist in oldDict.iteritems(): for value in valuelist: try: entry = invertedDict[value] if key not in entry: entry.append(key) except KeyError: invertedDict[value] = [key] return invertedDict The original is a dict of lists, and the result is a dict of lists. This "inverts" it.