Get list of subset from infinite generator in Python -
summary : I try to learn about itertools.islice I am doing I am trying to find the best way to get a list made from a subset of returns from an unlimited generator function. For example, I want a list of 1,000th through 1000th item from the generator. This is my example generator: These values are return returns from generators which I want to start and stop the list: Method 1 : Calculate the list (failed) This will never come back clearly, when you expand into it: Method 2 : list () (task) this works, but in fact It looks like a hack too hard to follow, although I was mistakenly thought that this method would be faster than 3. Method 3 : Easy Way < Strong> (work) Method 4 : itertools.takewhile (works) Method 5 : Wrapping generator (task) As expected, method is significantly slower than 2 and 3. Overall, I was surprised that the method 3 is very close to the time of method 2. This is more code, but anyone is very easy to follow Edit: Method: How can this method be implemented 6 itertools.islice (winner) This is a little faster than my starting itertools. Legislative solution with list precision: Val (infinite_counter (), start_ind, end_ind)] for Val in [itertools.islice] Method 6 = Time of the unit Method 2 ~ = 2.5 * Time of Unit Method 3 ~ = 3 * Unit Time Method 4 ~ = 4.2 Time of Unit Method 5 ~ = 4 * Unit Time this Which Easily That sounds fine to me.
def infinite_counter (): i = 0 While this is true: i + = 2 yield i
start = 1000 end = 2000
[for Val Ind, Val (NIMX) calculate (infinite_counter ()) if start < Lt; = Ind & lt; = End]
For Ind, calculate value (infinite_counter) ()): If started & lt; Ind end lieutenant; Initial:
list (next (IRAR ([]) If Val, Enumerate (Endless Sequire ()) for Ind & GT and Inds, if Ind starts, then
my_list = [], count in value (infinite_counter ()): if ind & gt; = start: my_list.append (val) if ind & gt; = Before I do this myself before being dragonless, it is the first way to think before doing this: I was surprised that this time was about 2 in 2 method. As it was.
[ind for val, Itertools.takewhile (Lambda Tup: Tup [0] & lt; End, enumerate (infinite_counter ())) if ind & gt; First of all, I thought that until Lambda was in the form of "Lambda Ind, Wall:" it did not work while taking it. But it gives a tube of two values to lambda. As an index for initial exit, I need to take the first term in Tupal. This method is slower than 2 and 3, and is slow as of approximately 5 methods.
def top_ending_generator (end): For Ind, value Calculation (infinite_counter ()): if ind & gt; End brake yield ind, val for ind, wall in top_ending_ operator (end) if ind and gt; Beginning]
list (itertools.islice (infinite_counter (), start, end))
list (next (ITR ([i.e. ([ ]) If there is other value for Ind & GT Industries, calculate the value (infind_counter ()) if ind & gt; = start)
< Code> def _secret (): For Ind, calculate the value (infinite_counter ()): if ind & gt; = Start: if ind and g; Intermediate yield list (next (itter ([]) Other: Product Value List (_secret ())
Def_secret ( ): For Ind, calculate the value (infinite_counter ()): If End End Lieutenant: Start: Continue if Ind & GT; End Brake Generation Wal List (_secret ())
Comments
Post a Comment