gpt4 book ai didi

python - python : Noob IndexError

转载 作者:行者123 更新时间:2023-12-03 09:04:20 25 4
gpt4 key购买 nike

我目前正在尝试创建一个可以扫描字符串的代码,将找到的每个字母的位置放在与该字母相关联的列表中(例如:如果您将S作为字符串的35、48和120字母,它将将35、48和120放在字母S的列表中)。然后,它将将此列表作为带有S的键的值放入字典中。

我的问题很简单,当我尝试将值放在列表中时,我遇到了IndexError:列表分配索引超出范围,但是我找不到原因。

string = "Squalalanoussommespartisetjetedteste"
taille = len(string)
dico = dict()
dico = {}
i = 0
for i in range(taille):
if string[i] == "A" or string[i] == "a" :
va = 0
valA = []
valA[va] = i
va = va + 1
print(valA)

我为我的英语不好而表示歉意,并在此先感谢您的帮助。

最佳答案

将项目推送到python中的列表时,无需指定索引。尝试这个:

for i in range(taille):
if string[i] == "A" or string[i] == "a" :
valA = []
valA.append(i)
print(valA)

关于python - python : Noob IndexError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35984539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com