简体   繁体   English

类型错误:列表索引必须是整数,而不是 str(在我的代码中,Python)

[英]TypeError: list indices must be integers, not str (in my code,Python)

vvod = input('Input: ')
vvod_a = []
for i in vvod:
    vvod_a.append(i)
print (vvod_a)
for i in vvod_a:
    if i == 'a' or 'b' or 'c':
        vvod_a[i] = 'bg'

print (vvod_a)

Traceback (most recent call last):
['f', 'e', 'f', 's', 'f', 'f', 'a', 'a', 'f', 'a', 'e']
  File "/home/n4/Рабочий Влад/PYTHON/coding", line 8, in <module>
    vvod_a[i] = 'bg'
TypeError: list indices must be integers, not str

Help me please,if i in list 'a' or 'b' or 'c', then i will be 'bg'.请帮助我,如果我在列表 'a' 或 'b' 或 'c' 中,那么我将是 'bg'。

First, you ask for input() , producing a string.首先,您请求input() ,生成一个字符串。 Then you do for i in vvod: vvod_a.append(i) to turn this string into a list of 1-character strings (for future reference, you can do this with vvod_a = list(vvod) ).然后你for i in vvod: vvod_a.append(i)for i in vvod: vvod_a.append(i)把这个字符串变成一个 1 个字符的字符串list (为了将来参考,你可以用vvod_a = list(vvod)做到这一点)。 This produced the list : ['f', 'e', 'f', 's', 'f', 'f', 'a', 'a', 'f', 'a', 'e'] .这产生了list['f', 'e', 'f', 's', 'f', 'f', 'a', 'a', 'f', 'a', 'e'] . Then you attempt to modify some of these with if i == 'a' or 'b' or 'c': , but this is equivalent to if (i=='a') or ('b') or ('c') , and since a non-empty string is truthy, you then get a condition that's always true.然后您尝试使用if i == 'a' or 'b' or 'c':修改其中的一些,但这等效于if (i=='a') or ('b') or ('c') ,并且由于非空字符串为真,因此您会得到一个始终为真的条件。

Then we get to the actual problem: vvod_a[i] = 'bg' .然后我们进入实际问题: vvod_a[i] = 'bg' You are iterating over the actual elements in the list , not its indices.您正在迭代list实际元素而不是它的索引。 i is a string, not an integer that could be used for list indexing. i是一个字符串,而不是可用于list索引的整数。

I recommend the following minimal changes:我建议进行以下最小更改:

vvod = input('Input: ')
vvod_a = []
for i in vvod:
    vvod_a.append(i)

print (vvod_a)
for i in range(len(vvod_a)):
    if vvod_a[i] in 'abc':
        vvod_a[i] = 'bg'

print (vvod_a)

If you want to tighten up your code, you could use a dictionary combined with the get method in a list comprehension:如果你想收紧你的代码,你可以在list理解中结合使用字典和get方法:

vvod = input('Input: ')
change_map = {'a':'bg', 'b':'bg', 'c':'bg'}
vvod_a = [change_map.get(item, item) for item in vvod]
print(vvod_a)

As TigerhawkT3 has pointed out, the two main problems in your code are that in the for loop you use the actual elements of the list as index ( vvod_a[i] in line 8), and that the if condition in line 7 fails to compare the expression in the left-hand side of the condition with each of the elements in the right-hand side.正如 TigerhawkT3 指出的那样,您代码中的两个主要问题是,在for循环中,您使用列表的实际元素作为索引(第 8 行中的vvod_a[i] ),以及第 7 行中的if条件无法比较条件左侧的表达式,右侧的每个元素。 As an alternative solution, I would suggest the use of enumerate():作为替代解决方案,我建议使用 enumerate():

vvod = input('Input: ')
vvod_a = []
for i in vvod:
     vvod_a.append(i)
print (vvod_a)
for index, i in enumerate(vvod_a):
    if i == 'a' or i == 'b' or i == 'c':
        print(i)
        vvod_a[index] = 'bg'
        print(vvod_a)

print (vvod_a)

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

相关问题 &#39;TypeError:列表索引必须是整数,而不是str&#39;Python 3 - 'TypeError: list indices must be integers, not str' Python 3 类型错误:列表索引必须是整数,而不是 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:列表索引必须是整数,而不是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-&gt; 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 TypeError:列表索引必须是整数或切片,而不是代码的 str - TypeError: list indices must be integers or slices, not str for the code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM