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:

  def infinite_counter (): i = 0 While this is true: i + = 2 yield i   

These values ​​are return returns from generators which I want to start and stop the list:

  start = 1000 end = 2000   

Method 1 : Calculate the list (failed)

  [for Val Ind, Val (NIMX) calculate (infinite_counter ()) if start < Lt; = Ind & lt; = End]   

This will never come back clearly, when you expand into it: For Ind, calculate value (infinite_counter) ()): If started & lt; Ind end lieutenant; Initial:

Method 2 : list () (task)

  list (next (IRAR ([]) If Val, Enumerate (Endless Sequire ()) for Ind & GT and Inds, if Ind starts, then   

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)

  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.  

Method 4 : itertools.takewhile (works)

  [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.  

Method 5 : Wrapping generator (task)

  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]   

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)

  list (itertools.islice (infinite_counter (), start, end))   

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


Import from Istaltlaw Please note that this is
  list (next (ITR ([i.e. ([ ]) If there is other value for Ind & GT Industries, calculate the value (infind_counter ()) if ind & gt; = start)   

this

 < 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 ())   

Which Easily

  Def_secret ( ): For Ind, calculate the value (infinite_counter ()): If End End Lieutenant: Start: Continue if Ind & GT; End Brake Generation Wal List (_secret ())   

That sounds fine to me.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -