TypeError:只能连接列表(不是" int")到列表[使用字典]

时间:2017-10-23 18:25:03

标签: python dictionary

基本上,我通过使用字典创建一个计数,每次它看到" 1"在文本文件中,它向数组添加一个。但是,我一直收到错误

../../../../hero-data.ts

BTW,文本文件格式如下;

Letters = ["A","B,"C","D","E","F"]
d= {}
d["A"] = [0]
d["B"] = [0]
d["C"] = [0]
d["D"] = [0]
d["E"] = [0]
file = open('test1.txt','r')
for line in file:
    line_array = line.strip("\n").split(",")
    for x in range(5):
        if line_array[x] == "1":
            for y in Letters:
                d[y][0] = d[y][0] + 1
编辑抱歉,错误

1 个答案:

答案 0 :(得分:1)

你实际上从未使用过你的字典。

Letters= ["A","B","C","D","E"]
d= {key: 0 for key in Letters}
print(Letters)
file = open('test1.txt','r')
for line in file:
    line_array = line.strip("\n").split(",")
    for x in range(5):
        if line_array[x] == "1":
            for i, value in enumerate(Letters):
                if i == x:
                    d[value] = d[value] + 1
#print(candidatescores) # No idea where this comes from