Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand you well, then you use the <code>range(0, 10000)</code> only to simulate the sequence. Whenever you use the <code>yield</code> in a function definition, it becomes a <em>generator function</em>. When generator function is used (called), it returns iterator -- you do not see it. Try the following <code>gen()</code> instead:</p> <pre><code>def gen(n): while n &gt; 0: yield n n -= 1 # decrement the value </code></pre> <p>Then you typically use it in a loop:</p> <pre><code>for x in gen(10000): print x, # comma to suppress new line </code></pre> <p>If you have a function that returns your strings, just <code>yield s</code> instead of building the list. Then call the generator function and collect only the values that you want (here not strings but numbers divisible by 100):</p> <pre><code>lst = [] # init for x in gen(10000): if x % 100 == 0: lst.append(x) print lst </code></pre> <p>The loop can be also replaced by so called list comprehension:</p> <pre><code>lst = [ x for x in gen(10000) if x % 100 == 0 ] print lst </code></pre> <p>...which is more understandable (i.e. resembles the above loop) when you write the same like that:</p> <pre><code>lst = [ x for x in gen(10000) if x % 100 == 0 ] print lst </code></pre> <p>But you can use a generator function wherever a sequence is expected. If your generator can implement the decision whether your element is to be collected, then you can simply make a list of the produced elements like this:</p> <pre><code>lst = list(gen(100)) print lst </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload