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

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

TypeError: list indices must be integers, not str Python

pythonmapreduce

提问by kerschi

list[s]is a string. Why doesn't this work?

list[s]是一个字符串。为什么这不起作用?

The following error appears:

出现以下错误:

TypeError: list indices must be integers, not str

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

list = ['abc', 'def']
map_list = []

for s in list:
  t = (list[s], 1)
  map_list.append(t)

采纳答案by GLHF

list1 = ['abc', 'def']
list2=[]
for t in list1:
    for h in t:
        list2.append(h)
map_list = []        
for x,y in enumerate(list2):
    map_list.append(x)
print (map_list)

Output:

输出:

>>> 
[0, 1, 2, 3, 4, 5]
>>> 

This is what you want exactly.

这正是你想要的。

If you dont want to reach each element then:

如果您不想到达每个元素,则:

list1 = ['abc', 'def']
map_list=[]
for x,y in enumerate(list1):
    map_list.append(x)
print (map_list)

Output:

输出:

>>> 
[0, 1]
>>> 

回答by Hackaholic

it should be:

它应该是:

for s in my_list:     # here s is element  of list not index of list
    t = (s, 1)
    map_list.append(t)

i think you want:

我想你想要:

for i,s in enumerate(my_list):  # here i is the index and s is the respective element
    t = (s, i)
    map_list.append(t)

enumerategive index and element

enumerate给出索引和元素

Note: using list as variable name is bad practice. its built in function

注意:使用列表作为变量名是不好的做法。它的内置功能

回答by Ujjwal

Do not use the name listfor a list. I have used mylistbelow.

不要将名称list用于列表。我在mylist下面用过。

for s in mylist:
    t = (mylist[s], 1)

for s in mylist:assigns elements of mylistto si.e stakes the value 'abc' in the first iteration and 'def' in the second iteration. Thus, scan't be used as an index in mylist[s].

for s in mylist:将元素分配mylistsies在第一次迭代中取值 'abc',在第二次迭代中取值 'def'。因此,s不能用作mylist[s].

Instead, simply do:

相反,只需执行以下操作:

for s in lists:
    t = (s, 1)
    map_list.append(t)
print map_list
#[('abc', 1), ('def', 1)]

回答by NPE

When you iterate over a list, the loop variable receives the actual list elements, not their indices. Thus, in your example sis a string (first abc, then def).

当您迭代一个列表时,循环变量接收实际的列表元素,而不是它们的索引。因此,在您的示例中s是一个字符串(首先abc,然后def)。

It looks like what you're trying to do is essentially this:

看起来您要做的基本上是这样的:

orig_list = ['abc', 'def']
map_list = [(el, 1) for el in orig_list]

This is using a Python construct called list comprehension.

这是使用名为list comprehension的 Python 构造。

回答by Danny Staple

for s in listwill produce the items of the list and not their indexes. So swill be 'abc'for the first loop, and then 'def'. 'abc'could only be a key to a dict, not a list index.

for s in list将生成列表的项目而不是它们的索引。所以s'abc'第一个循环,然后 'def''abc'只能是字典的键,而不是列表索引。

In the line with tfetching the item by index is redundant in python.

tpython 中,通过索引获取项目是多余的。