Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The random specific problem can easily be solved like this:</p> <pre><code>samples = [ random.random()/2 + .5 for x in range(10) ] </code></pre> <p>But I think you're question is aimed to address the specific problem where you want to execute the test on a generated item. To do this you can use multiple list comprehensions, or even better, use a generator and a list comprehension to limit the number of iterations:</p> <pre><code>samples = (random.random() for x in range(10)) samples = [x for x in samples if x &gt; .5] </code></pre> <p>Note that you can insert other generator-based filters between the first generator and the last list comprehension. This will let you do whatever you want in one single pass. Consider these interesting presentations on the topic: <a href="http://www.dabeaz.com/generators/" rel="nofollow">http://www.dabeaz.com/generators/</a> and <a href="http://www.dabeaz.com/coroutines/" rel="nofollow">http://www.dabeaz.com/coroutines/</a> (in the second one the author finishes up by implementing a simple operating system by using coroutines and generators). </p> <p>Now, this code does not address the fact that you want exactly 10 items. Let me try to find and code up an example and edit this answer (edit: just saw your comment about "10 or less", so this already replies to your question... I may anyway try to find a solution which gives you 10 items, just for fun :) ).</p> <p><strong>Edit:</strong></p> <p>I came up with the following solution. It is not purely LC/generator based as it uses the <code>itertools</code> module, but I though it will be interesting to post:</p> <pre><code>import itertools import random samples = (random.random() for _ in itertools.count()) samples = (x for x in samples if x &gt; .5) samples = [next(samples) for _ in range(10)] </code></pre> <p>Though, you can easily replace the <code>itertools.count</code> function with the following code:</p> <pre><code>def iiter(): while True: yield None </code></pre> <p>All this does not make much sense, you can get to the desired result without all this thinking by simply constructing you own generator, but list comprehension and generators are two of my favorite python features!</p>
    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.
    3. 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