如何在 Python 中组合嵌套循环的结果
How to combine the result of nested loop in Python
我需要帮助来组合两次迭代的嵌套循环输出。
这是我的嵌套代码:
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)
这是结果:
List of bit: [1, 0, 1, 1, 0]
End of iteration 1
List of bit: [1, 0, 0, 1, 1]
End of iteration 2
不过,我想合并循环的结果。据推测,结果可能会产生这样的东西:
Combination of bit:[1, 0, 1, 1, 0 ,1 , 0, 0, 1, 1]
希望有人可以帮助我做到这一点。
非常感谢。
绝对应该重组此代码,但这是解决方案。
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)))
输出
[1, 0, 1, 1, 0 ,1 , 0, 0, 1, 1]
只需在循环外初始化 numbers
,而不是每次迭代都清除它,这样您的结果就可以继续附加到 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)
您的 numbers
变量正在外循环中重新初始化。
其他答案已经指出了您的错误所在,但实际上对于这么简单的事情来说,代码太多了。 pythonic 方式是一种更简单、更易读的单行代码:
numbers = [random.randint(0,1) for i in range(10)]
考虑到代码只是创建了一个包含 10 个随机二进制值的列表,代码看起来非常复杂。您可以通过以下方式获得相同的效果:
>>> import random
>>> [random.choice([0,1]) for _ in range(10)]
[0, 0, 0, 0, 1, 0, 1, 0, 1, 1]
然而,由于代码的存在,每次迭代产生的值列表在下一次迭代开始时被行 numbers = []
.
丢弃
要么将它移到初始 while
语句之前,要么在 while 语句之外创建一个单独的列表并将每次迭代附加到它。
后一种方法(对代码进行最少的更改)如下所示:
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
我需要帮助来组合两次迭代的嵌套循环输出。 这是我的嵌套代码:
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)
这是结果:
List of bit: [1, 0, 1, 1, 0]
End of iteration 1
List of bit: [1, 0, 0, 1, 1]
End of iteration 2
不过,我想合并循环的结果。据推测,结果可能会产生这样的东西:
Combination of bit:[1, 0, 1, 1, 0 ,1 , 0, 0, 1, 1]
希望有人可以帮助我做到这一点。 非常感谢。
绝对应该重组此代码,但这是解决方案。
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)))
输出
[1, 0, 1, 1, 0 ,1 , 0, 0, 1, 1]
只需在循环外初始化 numbers
,而不是每次迭代都清除它,这样您的结果就可以继续附加到 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)
您的 numbers
变量正在外循环中重新初始化。
其他答案已经指出了您的错误所在,但实际上对于这么简单的事情来说,代码太多了。 pythonic 方式是一种更简单、更易读的单行代码:
numbers = [random.randint(0,1) for i in range(10)]
考虑到代码只是创建了一个包含 10 个随机二进制值的列表,代码看起来非常复杂。您可以通过以下方式获得相同的效果:
>>> import random
>>> [random.choice([0,1]) for _ in range(10)]
[0, 0, 0, 0, 1, 0, 1, 0, 1, 1]
然而,由于代码的存在,每次迭代产生的值列表在下一次迭代开始时被行 numbers = []
.
要么将它移到初始 while
语句之前,要么在 while 语句之外创建一个单独的列表并将每次迭代附加到它。
后一种方法(对代码进行最少的更改)如下所示:
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