Python 创建一个空列表以在之后分配数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4185462/
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
Creating an empty list to have data assigned afterwards
提问by Hypergiant
Lets say that I want to create a list of names
假设我想创建一个名称列表
listOfNames = []
Then I have a while loop such as
然后我有一个while循环,例如
count = 0
while listOfNames[count] < 10:
nameList[count] = raw_input("Enter a name: ")
count += 1
Python says that the list index is out of range, but the list index should be at 0, correct?
Python 说列表索引超出范围,但列表索引应该为 0,对吗?
How do add to the list each time the while loop is ran? I'm assuming something is wrong with my list assignment.
每次运行 while 循环时如何添加到列表中?我假设我的列表分配有问题。
采纳答案by Vincent Savard
An empty list doesn't have any index yet, it's an empty list! If you want to fill it, you have to do something like this :
一个空列表还没有任何索引,它是一个空列表!如果你想填充它,你必须做这样的事情:
while count < 10:
nameList.append(raw_input("Enter a name: "))
count += 1
This way, your condition will be evaluated and with the append method, you will be able to add new items in your list.
这样,您的条件将被评估,并且使用 append 方法,您将能够在列表中添加新项目。
There are a few advanced tricks to do this easily, but considering your level, I think it's better that you understand this code before moving on.
有一些高级技巧可以轻松做到这一点,但考虑到你的水平,我认为在继续之前你最好理解这段代码。
回答by J V
count = 0
while listOfNames[count] < 10:
nameList.append(raw_input("Enter a name: "))
count += 1
Use append for quick n easy...
使用 append 快速轻松...
Alternatively:
或者:
nameList[len(nameList):] = [raw_input("Enter a name: ")]
Edit:Did you mean listOfNames to be appended as opposed to nameList?
编辑:您的意思是要附加 listOfNames 而不是 nameList 吗?
回答by Andrew Jaffe
Most idiomatic is to use a list comprehension:
最惯用的是使用列表推导式:
namelist = [raw_input("Enter a name: ") for i in range(10)]
(or use _for i, although I personally wouldn't)
(或使用_for i,虽然我个人不会)
This has the advantage of creating the list on the fly, while having to use neither the append() method nor explicit indexing.
这具有动态创建列表的优点,同时不必使用 append() 方法或显式索引。
回答by redrah
Your list assignment is right, your problem is with your use of indexes which do not yet exist.
您的列表分配是正确的,您的问题在于您使用了尚不存在的索引。
count = 0
while listOfNames[count] < 10:
nameList[count] = raw_input("Enter a name: ")
count += 1
I'm fairly sure this code doesn't do what you intend. What this code is doing is checking the first through tenth elements of listOfNames for a number which is less than 10, but since its an empty list there is no element with index 0, or any other index for that matter hence your list index out of range exceptions.
我很确定这段代码没有按照你的意图去做。这段代码所做的是检查 listOfNames 的第一个到第十个元素是否有小于 10 的数字,但由于它是一个空列表,因此没有索引为 0 或任何其他索引的元素,因此您的列表索引为范围异常。
The following will work as you intend:
以下将按您的意愿工作:
count = 0
while len(listOfNames) < 10: # Keep going until the list has 10 elements
nameList.append(raw_input("Enter a name: "))
count += 1
However I'd suggest using the following which does exactly the same thing but should be more efficient as well as being more aesthetically pleasing:
但是,我建议使用以下内容,它可以做完全相同的事情,但应该更有效并且更美观:
for _ in range(10):
listOfNames.append(raw_input("Enter a name:"))
Note the use of append instead of an index reference. This will add the new element on to the end of the list whereas using the index as you were trying to do will fail since to assign to and index there has to be an element present in the first place.
请注意使用 append 而不是索引引用。这会将新元素添加到列表的末尾,而使用您尝试做的索引将失败,因为分配给和索引必须首先存在一个元素。

