Python can';t使用熊猫,TypeError:';int';对象不可下标

Python can';t使用熊猫,TypeError:';int';对象不可下标,python,Python,首先,我是一名python程序员新手,对堆栈溢出也不熟悉。如果我做错了什么,我会道歉 我有一个硬件问题,我正在读取一个csv文件,其中包含投票数据,每个人的投票。我应该使用循环(不能使用熊猫)来计算每个候选人的投票总数。我使用for循环遍历每个项,然后使用if语句查看候选项是否在求和列表中。投票者数据的主列表是poll_reader。候选名单在第三列。我为每个候选人创建的列表是候选人,候选人在第一列。当我运行它时,我得到的错误是: TypeError:“int”对象不可下标 引用If语句行。我以

首先,我是一名python程序员新手,对堆栈溢出也不熟悉。如果我做错了什么,我会道歉

我有一个硬件问题,我正在读取一个csv文件,其中包含投票数据,每个人的投票。我应该使用循环(不能使用熊猫)来计算每个候选人的投票总数。我使用for循环遍历每个项,然后使用if语句查看候选项是否在求和列表中。投票者数据的主列表是poll_reader。候选名单在第三列。我为每个候选人创建的列表是候选人,候选人在第一列。当我运行它时,我得到的错误是:

TypeError:“int”对象不可下标

引用If语句行。我以为我引用的数据是正确的,但显然不是。我是否在if语句中引用了错误的数据?这是我的密码

# Import modules needed, OS and CSV
import os
import csv

# create the path name to access the bank data in a csv file and use it
pollpath = os.path.join( 'Resources', 'election_data.csv')
candidates = ['Ro', 2]

# open the csv to begin looping through
with open(pollpath) as polldata:
    poll_reader = csv.reader(polldata, delimiter = ',')
    # skip the header row
    header = next(poll_reader)
    
    # initiate candidates list. 
    # Column [0] will be candidate name, column [1] will be votes
    already_listed = False
    
    # read the rows of each voters choice
    for row in poll_reader:
        # start with a for loop to then turn into a list comprehension.
        # Read through the netflix_reader data row by row,
        # use an if to test if the data is in the list of candidates, 
        # then sum their votes, whether existing or not
        # candidate or a new one
        for cndidt in candidates:
            print(cndidt[0])
#             if cndidt[0] == row[2]:
#                 candidates[1] += 1
#                 already_listed = True
#                 break
#             else:
#                 already_listed = False
#         if already_listed == False:
#             candidates[0].append(row[2])
#             candidates[1] += 1
#             break

问题是“cndidt的类型是什么?”显然,它不是可以订阅的东西(您可以应用
[x]
)。你为什么不把这一行
print(type(cndidt))
放在有问题的那一行之前,看看你得到了什么。你能把剩下的代码贴在你为候选人收集数据的地方吗?也许这就是问题的根源。候选列表的设置不正确或数据不正确,超出了您的预期。请发布文件的外观,或做一个最低限度的示例。是的,我发现,当我在数据中循环时,候选列表对我来说是一维的。我将编辑并输入整个代码。Jsut用我所有的代码更新了原始帖子