0

I need help to do the combination of nested loop output from two iteration. This is my nested while code:

iteration=0
while (iteration < 2):
   count = 0
   bit=5
   numbers = []
   while (count < bit):
      Zero = 0
      One = 1

      IV=(random.choice([Zero, One]))
      numbers.append(IV)
      count= count + 1
   print ('List of bit:', numbers)
   iteration=iteration + 1
   print ("End of iteration",iteration)

And this is the result:

List of bit: [1, 0, 1, 1, 0]
End of iteration 1
List of bit: [1, 0, 0, 1, 1]
End of iteration 2

However, I would like to combine the result of the loop. Supposedly, the result may produce something like this:

Combination of bit:[1, 0, 1, 1, 0 ,1 , 0, 0, 1, 1]

Hopefully someone may help me to do this. Thank you so much.

0

5 Answers 5

4

This code should definitely be reorganized, but here is the solution.

from itertools import chain

# list for appending results
combined = []    

iteration=0
while (iteration < 2):
    count = 0
    bit=5
    numbers = []
    while (count < bit):
        Zero = 0
        One = 1

        IV=(random.choice([Zero, One]))
        numbers.append(IV)
        count= count + 1
    print ('List of bit:', numbers)
    iteration=iteration + 1
    print ("End of iteration",iteration)

    # append the result
    combined.append(numbers)

# print the combined list
print(list(chain.from_iterable(combined)))

Output

[1, 0, 1, 1, 0 ,1 , 0, 0, 1, 1]
Sign up to request clarification or add additional context in comments.

Comments

2

Simply initialize numbers outside the loop, instead of clearing it for every iteration, so that your results can keep appending to numbers.

iteration=0
numbers = []
while (iteration < 2):
   count = 0
   bit=5
   while (count < bit):
      Zero = 0
      One = 1

      IV=(random.choice([Zero, One]))
      numbers.append(IV)
      count= count + 1
   print ('List of bit:', numbers)
   iteration=iteration + 1
   print ("End of iteration",iteration)

1 Comment

OP might be doing that intentionally.
1

Given that the code simply creates a list of 10 random binary values, the code seems extremely complex. You could get the same effect with the following:

>>> import random
>>> [random.choice([0,1]) for _ in range(10)]
[0, 0, 0, 0, 1, 0, 1, 0, 1, 1]

However, as the code stands the list of values produced each iteration is thrown away at the start of the next iteration by the line numbers = [].

Either move this before the initial while statement, or create a separate list outside the while statement and append each iteration to it.

This latter approach (with minimal changes to your code) would look like this:

iteration=0
all_numbers = [] # List to hold complete set of results

while (iteration < 2):
   count = 0
   bit=5
   numbers = []
   while (count < bit):
      Zero = 0
      One = 1

      IV=(random.choice([Zero, One]))
      numbers.append(IV)
      count= count + 1
   print ('List of bit:', numbers)
   iteration=iteration + 1
   print ("End of iteration",iteration)
   all_numbers.extend(numbers)  # Add the iteration to the complete list

print ('Complete list', all_numbers) # Show the aggregate result

Comments

0

your numbers variable is being re-initialized in the outer loop.

Comments

0

Other answers already pointed where your error was, but really that's WAY too much code for such a simple thing. The pythonic way is a much simpler and more readable one-liner:

numbers =  [random.randint(0,1) for i in range(10)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.