Creating a list where a certain char occur a set number of times
<p>I am creating a list in python 2.7
The list consists of 1's and 0's however I need the 1's to appear randomly in the list and a set amount of times.</p>

<p>Here is a way I found of doing this however can take a long time to create the list</p>

<pre><code>numcor = 0




while numcor != (wordlen):    #wordlen being the set amount of times
    usewrong = []

    for l in list(mymap):

        if l == "L": #L is my map an telling how long the list needs to be
            use = random.choice((True, False))
            if use == True:
                usewrong.append(0)
            else:
                usewrong.append(1)
                numcor = numcor + 1
</code></pre>

<p>Is there a more effiecient way of doing this?</p>

<p>Simpler to way to create the list with <code>0</code>s and '1's is:</p>

<pre><code>&gt;&gt;&gt; n, m = 5, 10
&gt;&gt;&gt; [0]*n + [1]*m
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
</code></pre>

<p>where <code>n</code> is the number of <code>0</code>s and <code>m</code> is the number of <code>1</code>s</p>

<p>However if you want <em>the list to be shuffled in random order</em>, you may use <a href="https://docs.python.org/2/library/random.html#random.shuffle" rel="nofollow"><code>random.shuffle()</code></a> as:</p>

<pre><code>&gt;&gt;&gt; from random import shuffle
&gt;&gt;&gt; mylist = [0]*n + [1]*m  # n and m are from above example
&gt;&gt;&gt; shuffle(mylist)
&gt;&gt;&gt; mylist
[1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1]
</code></pre>

python arrays list python-2.7