小编stu*_*ent的帖子

python字典,保持整数计数

我试图计算一个说,整数列表.我有一个csv文件中的数字列表,我能够读入,看起来像4,245,34,99,340,...我正在做的是尝试返回的是一个字典,其中键:值对,其中键是csv文件中的整数值,该值是它在列表中显示的次数.我不确定我在这里做错了什么,任何帮助都会受到赞赏

allCounts = dict()

rows = csv.reader(open('...csv'), delimiter=',')

    for intValue in rows:
        intVal = intValue[0]

        for intVal, numAppearances in allCounts:
             if intVal in allCounts:
                allCounts[numAppearances] = allCounts[numAppearances]+1
             else:
                allCounts[numAppearances] = 1
Run Code Online (Sandbox Code Playgroud)

python dictionary

3
推荐指数
2
解决办法
3007
查看次数

标签 统计

dictionary ×1

python ×1