gpt4 book ai didi

python - 计算列表中对象的出现次数

转载 作者:太空宇宙 更新时间:2023-11-04 02:15:16 28 4
gpt4 key购买 nike

我有一个 .CSV 文件,其中包含来自网站的 LSAT 答案:

"Test","Question","Section","Question Type","Your Answer","Correct Answer"    
"PT 62","2: 1","LR","Best Principle for Example","D","D"
"PT 62","2: 2","LR","Strengthen","E","E"
"PT 62","2: 3","LR","Direct Logic Link","B","B"

... repeat 1,000x

我想开始从这个 .CSV 中获取数据,这样我就可以计算出“问题类型”中我答对的百分比(或多少)问题。

我查阅了 Python 手册,许多类似类型的论坛问题,以及许多 .count 答案,但似乎没有一个适合我正在尝试做的事情。

import csv
import itertools
import json
from collections import Counter

file = open('C:/Users/Kenny/Downloads/logicReasoning.csv')
reader = csv.reader(file)
data = list(reader)

masterList = []
questionTypes =[]

for row in data:
masterList.append(row[3])
for x in masterList:
c = Counter(x)
masterList.count(x)
print("total "+x+":", masterList.count(x))

输出

total Justify: 28

total Definition: 28

total Most Similar in Flawed Reasoning: 14

total Resolve Discrepancy: 24

etc, for each question type.

上面的代码打印出“问题类型”的列表,加上它在 masterList 中统计的次数,每次出现问题类型

这样,'Justify 28' 被打印 28 次,每次在 CSV 文件中出现一次。

我只想打印一次'Justify',它的总数在 CSV 文件中。

然后我将为“问题类型”重新编写相同的代码,这样我就可以创建一个新的空列表,并且仅在每个实例正确时才附加每个实例 - 给出如下:

if row[4] == row[5]:
correctList.append(row[3])

这是否是按问题类型计算总问题数以及按问题类型计算正确问题总数的正确方法,这样我就可以得出百分比和其他数据?

最佳答案

import csv
from collections import Counter

counter = Counter()
with open('lsat.csv') as fp:
for row in csv.reader(fp):
counter[row[3]] += 1

print(counter)
print(list(counter.keys()))
print(counter['Strengthen'])

关于python - 计算列表中对象的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52815136/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com