简体   繁体   English

TypeError:list indices必须是整数,而不是str Python

[英]TypeError: list indices must be integers, not str Python

list[s] is a string. list[s]是一个字符串。 Why doesn't this work? 为什么这不起作用?

The following error appears: 出现以下错误:

TypeError: list indices must be integers, not str TypeError:list indices必须是整数,而不是str

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

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

When you iterate over a list, the loop variable receives the actual list elements, not their indices. 迭代列表时,循环变量接收实际的列表元素,而不是它们的索引。 Thus, in your example s is 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构造。

Do not use the name list for a list. 不要将名称list用于列表。 I have used mylist below. 我在下面使用过mylist

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

for s in mylist: assigns elements of mylist to s ie s takes the value 'abc' in the first iteration and 'def' in the second iteration. for s in mylist:分配的元素mylistss取值“ABC”在第一次迭代和第二次迭代“DEF”。 Thus, s can't be used as an index in mylist[s] . 因此, s不能用作mylist[s]的索引。

Instead, simply do: 相反,只需:

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

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)

enumerate give index and element enumerate给索引和元素

Note: using list as variable name is bad practice. 注意:使用list作为变量名是不好的做法。 its built in function 它内置的功能

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]
>>> 

for s in list will produce the items of the list and not their indexes. for s in list将生成for s in list的项目而不是其索引。 So s will be 'abc' for the first loop, and then 'def' . 所以s对于第一个循环是'abc' ,然后是'def' 'abc' could only be a key to a dict, not a list index. 'abc'只能是dict的关键,而不是列表索引。

In the line with t fetching the item by index is redundant in python. t按索引获取项目的行在python中是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'TypeError:列表索引必须是整数,而不是str'Python 3 - 'TypeError: list indices must be integers, not str' Python 3 类型错误:列表索引必须是整数,而不是 str - python - TypeError: list indices must be integers, not str - python TypeError:列表索引必须是整数,而不是Python中的str - TypeError: list indices must be integers, not str in Python Python 类型错误:列表索引必须是整数,而不是 str - Python TypeError: list indices must be integers, not str Python:TypeError:list indices必须是整数,而不是str - Python: TypeError: list indices must be integers, not str Python-> TypeError:列表索引必须是整数,而不是str - Python -> TypeError: list indices must be integers, not str Python列表循环错误:TypeError:列表索引必须是整数,而不是str - Python List Loop Error: TypeError: list indices must be integers, not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List 使用Python解析JSON:TypeError:list indices必须是整数,而不是str - Parsing JSON with Python: TypeError: list indices must be integers, not str Python chatbot-TypeError:列表索引必须是整数,而不是str - Python chatbot - TypeError: list indices must be integers, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM