在Python 3中从嵌套的for循环输出值创建列表

时间:2018-11-08 15:08:21

标签: python-3.7

我嵌套了如下所示的for循环输出,并希望根据该输出值创建一个列表。请建议我如何实现此目标。

用于循环输出值:

Order_Numbers:
1234_90
3456_80
9876_70
2345_23

预期: 列表= [1234_90, 3456_80, 9876_70, 2345_23]

1 个答案:

答案 0 :(得分:0)

您可以将列表初始化为for循环外,并继续将值附加在“ for”循环内。伪代码如下,因为问题不是很清楚。

for .... (outer loops):
    order_number_list = [] #Initialize the list
    for.... (actual loop where the ordered number is created):
         order_number_list.append(number) #appends number to the created list
    print(order_number_list) #Doing this outside the innermost for loop is going to print expected output.