使用for循环基于特定的字符串值遍历字典并基于值匹配创建计数吗?

时间:2019-05-04 18:28:00

标签: python python-2.7

我有一本包含键/值对(键= ID#)和值(硼,等级)的字典。我需要计算字典中每个自治区出现的数字。目前,我有这个函数可以返回我要寻找的数量,但是我宁愿使用for循环返回“计数”值,而不是将所有值连接起来以得到当前代码所支持的结果...

    import csv 
    def get_score_summary(readfile):
    newdict = {}
    with open(readfile) as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            newdict[row['CAMIS']] = (row['BORO'], row['GRADE'])
       # return newdict
    csvfile.close()
    from collections import Counter
    Counter(newdict.values())
    output = Counter(newdict.values())
    bronxCount = output[("BRONX", "A")] + output["BRONX", "B"] + output["BRONX", "C"] + output["BRONX", "D"] + output["BRONX", "F"]
    queensCount = output[("QUEENS", "A")] + output["QUEENS", "B"] + output["QUEENS", "C"] + output["QUEENS", "D"] + output["QUEENS", "F"]
    manhattanCount = output[("MANHATTAN", "A")] + output["MANHATTAN", "B"] + output["MANHATTAN", "C"] + output["MANHATTAN", "D"] + output["MANHATTAN", "F"]
    statenCount = output[("STATEN ISLAND", "A")] + output["STATEN ISLAND", "B"] + output["STATEN ISLAND", "C"] + output["STATEN ISLAND", "D"] + output["STATEN ISLAND", "F"]
    brooklynCount = output[("BROOKLYN", "A")] + output["BROOKLN", "B"] + output["BROOKLYN", "C"] + output["BROOKLYN", "D"] + output["BROOKLYN", "F"]

 return bronxCount, queensCount, manhattanCount, statenCount, brooklynCount

get_score_summary(“ inspection_results.csv”)

(111、304、528、38、251)

0 个答案:

没有答案