如何在 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
How to create a dynamic list in Python?
提问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 x
and the append into this array x
inside a for loop
as 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)
.