尝试按索引访问列表时出现 Python 错误-“列表索引必须是整数,而不是 str”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14198821/
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-18 10:45:01  来源:igfitidea点击:

Python error when trying to access list by index - "List indices must be integers, not str"

python

提问by Tom Jenkins

I have the following Python code :

我有以下 Python 代码:

currentPlayers = query.getPlayers()
    for player in currentPlayers:
        return str(player['name'])+" "+str(player['score'])

And I'm getting the following error:

我收到以下错误:

TypeError: list indices must be integers, not str

类型错误:列表索引必须是整数,而不是 str

I've been looking for an error close to mine, but not sure how to do it, never got that error. So yeah, how can I transform it to integers instead of string? I guess the problem comes from str(player['score']).

我一直在寻找一个接近我的错误,但不知道该怎么做,从来没有遇到过那个错误。所以是的,我怎样才能将它转换为整数而不是字符串?我猜问题来自str(player['score']).

采纳答案by Spacedman

Were you expecting playerto be a dictrather than a list?

您是否希望player成为 adict而不是 a list

>>> player=[1,2,3]
>>> player["score"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> player={'score':1, 'age': 2, "foo":3}
>>> player['score']
1

回答by mgilson

playersis a list which needs to be indexed by integers. You seem to be using it like a dictionary. Maybe you could use unpacking -- Something like:

players是一个需要用整数索引的列表。你好像把它当作字典一样使用。也许你可以使用解包——比如:

name, score = player

(if the playerlist is always a constant length).

(如果player列表始终是恒定长度)。

There's not much more advice we can give you without knowing what queryis and how it works.

在不知道query它是什么以及它是如何工作的情况下,我们无法为您提供更多建议。

It's worth pointing out that the entire code you posted doesn't make a whole lot of sense. There's an IndentationErroron the second line. Also, your function is looping over some iterable, but unconditionally returning during the first iteration which isn't usually what you actually want to do.

值得指出的是,您发布的整个代码并没有多大意义。IndentationError第二行有一个。此外,您的函数正在循环一些可迭代的,但在第一次迭代期间无条件返回,这通常不是您真正想要做的。

回答by ken.ganong

player['score']is your problem. player is apparently a listwhich means that there is no 'score' element. Instead you would do something like:

player['score']是你的问题。player 显然是 alist这意味着没有“分数”元素。相反,您会执行以下操作:

name, score = player[0], player[1]
return name + ' ' + str(score)

Of course, you would have to know the list indices (those are the 0 and 1 in my example).

当然,您必须知道列表索引(在我的示例中是 0 和 1)。

Something like player['score']is allowed in python, but playerwould have to be a dict.

player['score']python 中允许类似的东西,但player必须是dict.

You can read more about both lists and dicts in the python documentation.

您可以在python 文档中阅读有关列表和字典的更多信息。

回答by AmbuSreedharan

A list is a chain of spaces that can be indexed by (0, 1, 2 .... etc). So if players was a list, players[0] or players[1] would have worked. If players is a dictionary, players["name"] would have worked.

列表是可以由 (0, 1, 2 .... etc) 索引的空间链。所以如果玩家是一个列表,玩家 [0] 或玩家 [1] 会起作用。如果玩家是字典,玩家[“姓名”] 会起作用。