1

名前とスコアの2種類のリストがあり、すべてのスコアが既存のリストに結合されるようにスコアを結合しようとしています

「リストのインデックスは 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))])
4

5 に答える 5

9

You have scores that are conceptually associated with names. This tells you that you're using the wrong data structure. In particular, the numbers represent scores that you're going to be adding up, or more to the point, counting up.

Python has a built-in tool for this.

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.

As for your error message, it means exactly what it says, and @BryanOakley already explained it thoroughly. But I need to point out that in programming, you must be precise, and consistent, and constantly pay attention to detail. Good habits avoid these errors because you are constantly thinking about exactly what you are doing and exactly what it means. Style conventions help, too:

  • Name your iterator after the thing that it actually contains. Since, in Python, iterating over a list actually gives you items from the list (not integer indices), use a name that suggests an actual list item, and not something bland like i.

  • Name your lists and other containers with a plural name, so that your code naturally describes what's going on. Thus, write things like for name in names:.

But I mention "attention to detail" because

  • When you are posting your code, don't type it out; copy and paste so that we can see exactly what you have.

  • Spelling counts. Typos hurt (cahterine).

  • Strings have quotes around them. Do not get strings confused with variable names.

于 2013-03-07T00:12:37.540 に答える
9

Counterそのようなデータをモジュールからクラスに配置することを検討することをお勧めしcollectionsます。例えば:

#! /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})

また、カールの答えを見てください。これをさらに単純化します。

于 2013-03-07T00:09:14.990 に答える
5

エラーメッセージを信頼してください:それはあなたに問題が何であるかを正確に伝えています。あなたがそうするときscore2[i]、「私は何ですか?」と自問してください。

このコンテキストでは、iはname2の要素です。あなたのサンプルコードはそれをとして示していますが[bob, cahterine, jim, will, lucy]、私はそれらが文字列であると推測しています。

于 2013-03-06T23:57:25.443 に答える
0

私はこれを機能させましたが、それがあなたが探しているものかどうかわかりませんか?

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)
于 2013-03-07T00:09:41.703 に答える
0
def sum(index, name):
    score2[index] += score1[name1.index(name)]


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

[sum(index, _name) for index, _name in enumerate(name2) if _name in name1 and _name in name2]

リスト内包表記を使用できます

于 2013-03-07T03:42:14.297 に答える