adding tuples to a list in an if loop (python)
<p>I'm working in python with symbulate for a probability course and running some simulations. </p>

<p>Setup: Two teams, A and B, are playing in a âbest of nâ game championship series, where n is an odd number. For this example, n=7, and the probability team A wins any individual game is 0.55. Approximate the probability that Team A wins the series, given that they win the first game.</p>

<p>Here is what I've got so far, which I think is along the right lines:</p>

<pre><code>model = BoxModel([1, 0], probs=[0.55, .45], size=7, replace=True)
test = model.sim(10000)

for x in range(0,10000):
    test1 = test[x]

    if test1[0] == 1:
         print (test1)

test1
</code></pre>

<p>The last two lines are where I'm having my difficulty. This 'for' and 'if' combination makes it so only the inputs that start with a '1' (i.e. Team A winning the first game) are displayed. I need to save these inputs into a table so that I can run some further testing on it.</p>

<p>How do I input the value of test1 into a table while those loops are running? Currently, test1 only outputs the x=10,000th value.</p>

<p>Edit: The "test" yields a list, 0-10000, of all the possible game outcomes.  I need a list that only has the game outcomes that start with a "1".</p>

<p>Edit2: Output of "test" (before I run a "for" or "if") look like: </p>

<pre><code>Index   Result

0   (1, 1, 1, 0, 0, 1, 1)
1   (0, 1, 0, 1, 1, 0, 0)
2   (1, 1, 1, 1, 0, 1, 0)
3   (0, 0, 1, 1, 1, 1, 1)
4   (0, 0, 0, 0, 0, 0, 0)
5   (1, 1, 0, 1, 0, 0, 1)
6   (0, 0, 1, 0, 1, 1, 1)
7   (0, 1, 0, 0, 0, 0, 1)
8   (1, 1, 0, 1, 0, 1, 0)
... ...
9999    (1, 1, 0, 1, 0, 0, 0)
</code></pre>

<p>I need a "test' (or another variable) to contain something that looks EXACTLY like that, but only contains lines that start with "1". </p>

<p>So you're looking to store the results of each test? Why not store them in a <code>list</code>?</p>

<pre><code>test1_results = []

for x in range(0,10000):
    test1 = test[x]
    # check if first element in sequence of game outcomes is a win for team A
    if test1[0] == 1:    # or '1' if you're expecting string
        test1_results.append(test1)
</code></pre>

<p>You can the run <code>print(test1_results)</code> to print the entire list of results, but if you want to print the first <code>n</code> results, do <code>print(test1_results[:n])</code>.</p>

<p>If you want your <code>if</code> statement in there, you will have to slightly adjust the placement. What does your <code>test</code> object look like? Could you give us a small sample?</p>

<p>edit: updated <code>if</code> statement to reflect comment below</p>

python