Python 列表索引必须是整数,而不是str

Python 列表索引必须是整数,而不是str,python,list,merge,indexing,Python,List,Merge,Indexing,我有两种类型的列表:姓名和分数,我正在尝试合并分数,以便将所有分数合并到现有列表中 我一直得到的列表索引必须是整数,而不是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] sco

我有两种类型的列表:姓名和分数,我正在尝试合并分数,以便将所有分数合并到现有列表中

我一直得到的列表索引必须是整数,而不是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))])

信任错误消息:它准确地告诉您问题所在。当你给2[i]打分时,问问自己我是什么


在本文中,i是name2的一个元素。即使您的示例代码显示为[bob、cahterine、jim、will、lucy],我猜这些都是字符串。

请相信错误消息:它会准确地告诉您问题所在。当你给2[i]打分时,问问自己我是什么


在本文中,i是name2的一个元素。即使您的示例代码显示它为[Bob,CaTyRin,吉姆,Wrand,露西],但我猜想这些都是字符串。

< P>您可能想考虑将这些数据从模块中放置在类中。例如:

#! /usr/bin/env python
from collections import Counter

name1 = ["jim", "bob", "john", "smith"]
score1 = [4,7,3,11]
name2 = ["bob", "cahterine", "jim", "will", "lucy"]
score2 = [6,12,7,1,4]

first_set = dict(zip(name1, score1))
second_set = dict(zip(name2, score2))

print Counter(first_set) + Counter(second_set)
这将打印以下内容:

Counter({'bob': 13, 'cahterine': 12, 'jim': 11, 'smith': 11, 'lucy': 4, 'john': 3, 'will': 1})

还有,看看。它更简化了这一点。

您可能想考虑将这些数据从模块中放置在类中。例如:

#! /usr/bin/env python
from collections import Counter

name1 = ["jim", "bob", "john", "smith"]
score1 = [4,7,3,11]
name2 = ["bob", "cahterine", "jim", "will", "lucy"]
score2 = [6,12,7,1,4]

first_set = dict(zip(name1, score1))
second_set = dict(zip(name2, score2))

print Counter(first_set) + Counter(second_set)
这将打印以下内容:

Counter({'bob': 13, 'cahterine': 12, 'jim': 11, 'smith': 11, 'lucy': 4, 'john': 3, 'will': 1})

还有,看看。它使这个问题变得更加简单。

我让它发挥作用,但我不知道这是否就是你想要的

name1= ['jim', 'bob', 'john', 'smith']
name2= ['bob', 'cahterine', 'jim', 'will', 'lucy']
score1= [4,7,3,11]
score2= [6,12,7,1,4]
def merge (name1,score1, name2,score2):
    for x in range(0,len(name1)):
        name2.append(name1[x])
    for x in range(0,len(score1)):
        score2.append(score1[x])
    print name2
    print score2
merge(name1,score1, name2,score2)

我有这个工作,但我不知道这是不是你要找的

name1= ['jim', 'bob', 'john', 'smith']
name2= ['bob', 'cahterine', 'jim', 'will', 'lucy']
score1= [4,7,3,11]
score2= [6,12,7,1,4]
def merge (name1,score1, name2,score2):
    for x in range(0,len(name1)):
        name2.append(name1[x])
    for x in range(0,len(score1)):
        score2.append(score1[x])
    print name2
    print score2
merge(name1,score1, name2,score2)

你的分数在概念上与名字相关。这表明您使用了错误的数据结构。特别是,这些数字代表的是你将要加起来的分数,或者更确切地说,是累积起来的分数

Python为此提供了一个内置工具

from collections import Counter

results_1 = Counter(jim = 4, bob = 7, john = 3, smith = 11)
results_2 = Counter(bob = 6, catherine = 12, jim = 7, will = 1, lucy = 4)

results_1 + results_2 # yes, it's really this easy.
至于您的错误消息,它的意思和它所说的完全一样,@BryanOakley已经详细解释了它。但我需要指出的是,在编程中,您必须精确、一致,并不断关注细节。良好的习惯可以避免这些错误,因为你总是在思考你在做什么,以及它到底意味着什么。样式约定也有帮助:

根据迭代器实际包含的内容命名迭代器。因为在Python中,遍历列表实际上会得到列表中的项目,而不是整数索引,所以使用一个表示实际列表项目的名称,而不是像i这样乏味的名称

使用复数名称命名列表和其他容器,以便代码自然地描述正在发生的事情。因此,在names中编写类似于for name的内容:

但我提到对细节的关注是因为

当你发布代码时,不要把它打印出来;复制并粘贴,以便我们能够准确地看到您拥有的内容

拼写很重要。打字错误伤害了凯特琳

字符串周围有引号。不要将字符串与变量名混淆


你的分数在概念上与名字相关。这表明您使用了错误的数据结构。特别是,这些数字代表的是你将要加起来的分数,或者更确切地说,是累积起来的分数

Python为此提供了一个内置工具

from collections import Counter

results_1 = Counter(jim = 4, bob = 7, john = 3, smith = 11)
results_2 = Counter(bob = 6, catherine = 12, jim = 7, will = 1, lucy = 4)

results_1 + results_2 # yes, it's really this easy.
至于您的错误消息,它的意思和它所说的完全一样,@BryanOakley已经详细解释了它。但我需要指出的是,在编程中,您必须精确、一致,并不断关注细节。良好的习惯可以避免这些错误,因为你总是在思考你在做什么,以及它到底意味着什么。样式约定也有帮助:

根据迭代器实际包含的内容命名迭代器。因为在Python中,遍历列表实际上会得到列表中的项目,而不是整数索引,所以使用一个表示实际列表项目的名称,而不是像i这样乏味的名称

使用复数名称命名列表和其他容器,以便代码自然地描述正在发生的事情。因此,在names中编写类似于for name的内容:

但我提到对细节的关注是因为

当你发布代码时,不要把它打印出来;复制并粘贴,以便我们能够准确地看到您拥有的内容

拼写很重要。打字错误伤害了凯特琳

字符串周围有引号。不要将字符串与变量名混淆

您可以使用列表理解


您可以使用列表理解功能

为什么不简单地将它们合并为:name1=name1+name2和score2=score1+score2?icanc:OP希望按名称合并分数。也就是说,结果列表中每个名字只应有一个条目,并且一个分数对应于输入列表中与该名字相关联的分数之和。@Wilduck啊,谢谢你的澄清。为什么不简单地将它们合并为:name1=name1+name2和score2=score1+score2?icanc:OP想要按名字合并分数。也就是说,结果列表中的每个名称应该只有一个条目,并且一个分数对应于输入列表中与该名称相关联的分数之和。@Wilduck啊,谢谢你的澄清。+1是很好的数据结构选择,比我快了几分钟。你不需要intermed
稍后,只需执行Counterzipname1,score1等@gnibler是的,但您需要Counterdictzipname1,score1。我只是想向OP展示这些步骤,这样更容易理解。当然,正如Karl的回答一样,你可以将整个过程简化得更多。+1对于良好的数据结构选择,比我快几分钟。你不需要中间dict,只需执行Counterzipname1,score1等@gnibler是的,但你需要Counterdictzipname1,score1。我只是想向OP展示这些步骤,这样更容易理解。当然,你可以把整个事情简化很多,就像卡尔的回答一样。