There is a way you could put a lazily evaluated caching wrapper around an infinite generator to make it look like an infinitely long list you could index into at will, but that deserves its own question and answer, and would have major performance implications. The expression list(gen) will never finish. gen = (i for i in range(10))īut type this into the repl and you get: > gen = (i for i in range(10))Īs pointed out, if the generator is infinitely long, you can't even convert it to a list. One would initially expect the following to print 4 twice. MonkeyLearn's WordCloud Generator is completely free, and equipped with artificial intelligence (AI) to deliver more accurate and unique results than other word cloud. Let's take a look at another example, based on the code from the question. It was one of the earliest word cloud generators on the scene and a favorite among word cloud users, so it definitely deserves a mention here. Once you start going through a generator to get the nth value in the sequence, the generator is now in a different state, and attempting to get the nth value again will return you a different result, which is likely to result in a bug in your code. I'll demonstrate the problem with a simple example in the python repl: > gen = (i for i in range(10)) ![]() They don't store their intermediate results anywhere, so you can't go backwards. The simple but naive approach is the simple one-liner: gen = (i for i in range(10))īut remember, generators aren't like lists. ![]() I'd argue against the temptation to treat generators like lists.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |