如何在 Python 中创建动态列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36812185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:23:06  来源:igfitidea点击:

How to create a dynamic list in Python?

pythonloops

提问by aNikhil

How to create a dynamic list in Python. I am trying to prepare a Array where the value will be pushed dynamically into it (using a loop)

如何在 Python 中创建动态列表。我正在尝试准备一个数组,其中的值将被动态推送到其中(使用循环)

回答by krishna Prasad

Try this one: Initialize with a variable xand the append into this array xinside a for loopas below:

尝试这一个:初始化具有可变x和追加到该阵列x内的for loop,如下:

x = []
n = input("enter length")
for i in range(1, int(n)):
    k=input("enter value")
    x.append(k) # push your entered value

print x

You can append any value in the loop as x.append(value).

您可以将循环中的任何值附加为x.append(value).