【问题标题】:create simple list within a while loop python [duplicate]在while循环python中创建简单列表[重复]
【发布时间】:2020-04-27 18:55:33
【问题描述】:

我想在 python 的 while 循环中创建一个简单的列表

我正在使用此代码

def get_list(input):
    create_cell = []
    for line in input:
        create_cell.append(line)
    return create_cell

x=0
c = [x,'a','b']
while x < 5:
    new_row = get_list(c)
    print (new_row)
    x = x + 1

它给出以下输出

[0, 'a', 'b']
[0, 'a', 'b']
[0, 'a', 'b']
[0, 'a', 'b']
[0, 'a', 'b']

我想要的输出是:

[0, 'a', 'b']
[1, 'a', 'b']
[2, 'a', 'b']
[3, 'a', 'b']
[4, 'a', 'b']

【问题讨论】:

    标签: python list function loops while-loop


    【解决方案1】:

    分配给x 不会改变c。您还需要更新它:

    while x < 5:
        new_row = get_list(c)
        print (new_row)
        x = x + 1
        c[0] = x
    

    【讨论】:

    • 感谢您的回答。原来是这个问题,现在好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 2018-07-20
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2012-07-03
    相关资源
    最近更新 更多