我有两种类型的列表:名称和分数,我正在尝试合并分数,以便将所有分数合并到现有列表中
我不断收到“列表索引必须是整数,而不是 str”错误
name1= [jim, bob, john, smith]
score1= [4,7,3,11]
name2= [bob, cahterine, jim, will, lucy]
score2= [6,12,7,1,4]
我希望结果是:
name1 = [jim, bob, john, smith, catherine, will, lucy]
score2 = [11, 13 ,3 ,11 ,12 ,1 ,4]
def merge(name1,score1, name2,score2):
for i in name2:
if i in name1:
indexed= name1.index(i)
score2[i] =score1[int(indexed)]+score2[i]
if i not in name1:
name1.append(i)
score1.append(score2[(name1.index(i))])