简体   繁体   English

如何计算列表中的多个项目

[英]How to count multiple items in a list

I have the following list: 我有以下列表:

data = [[2004,1,1,1,50], [2008,2,28,1,150],[1984,5,1,3,20],[1982,5,1,7,20], [1982,5,8,7,20]]

The data represents the Year, Month, Day, Day of week, count. 数据代表年,月,日,星期几,计数。

I want to obtain a dictionary of the total counts per day of the week. 我想获得一周中每天总计数的字典。 Something like this 像这样的东西

results = {1:200,
           2:0,
           3:20,
           4:0,
           5:0,
           6:0,
           7:40,
}

I believe the best way to do this, please correct me if i'm wrong, is to use collections.Counter. 我相信最好的方法,请纠正我,如果我错了,就是使用collections.Counter。 I abandoned this effort for a dictionary comprehension but have been unable to solve the problem 我放弃了这种努力以获得字典理解但却无法解决问题

solution = {(x,i) for x[3], i[4] in data}

Since you want to sum and not count it might be easier to use defaultdict : 由于您想要求和而不计数,因此使用defaultdict可能更容易:

from collections import defaultdict

data = [[2004,1,1,1,50], [2008,2,30,1,150],[1984,5,1,3,20],[1982,5,1,7,20], [1982,5,8,7,20]]

c = defaultdict(int)

for l in data:
    c[l[3]] += l[4]

print(c)
# defaultdict(<class 'int'>, {1: 200, 3: 20, 7: 40})

If you insist on having zero entries you can instantiate it before: 如果您坚持零条目,您可以在以下情况下实例化它:

from collections import defaultdict

data = [[2004,1,1,1,50], [2008,2,30,1,150],[1984,5,1,3,20],[1982,5,1,7,20], [1982,5,8,7,20]]

c = defaultdict(int)
c.update({d: 0 for d in range(1, 8)})
for l in data:
    c[l[3]] += l[4]
print(c)
# defaultdict(<class 'int'>, {1: 200, 2: 0, 3: 20, 4: 0, 5: 0, 6: 0, 7: 40})

At this point you may use a normal dict over defaultdict if you are sure that the input will not have invalid days: 此时,如果您确定输入没有无效天数,则可以使用普通dict而不是defaultdict

data = [[2004,1,1,1,50], [2008,2,30,1,150],[1984,5,1,3,20],[1982,5,1,7,20], [1982,5,8,7,20]]

c = {d: 0 for d in range(1, 8)} # or dict.fromkeys(range(1, 8), 0)
for l in data:
    c[l[3]] += l[4]
print(c)
# {1: 200, 2: 0, 3: 20, 4: 0, 5: 0, 6: 0, 7: 40}

If, as in your input data, your data is sorted by day of week, ie all sublists for a particular day of week are adjacent to each other, you can use itertools.groupby with a dictionary comprehension: 如果在输入数据中,您的data按星期几排序,即特定星期几的所有子列表彼此相邻,则可以使用itertools.groupby和字典理解:

from itertools import groupby
from operator import itemgetter

res = {k: sum(map(itemgetter(-1), v)) for k, v in groupby(data, key=itemgetter(-2))}

print(res)
# {1: 200, 3: 20, 7: 40}

If your data is not sorted, you will have to sort by day of week first : 如果您的数据没有排序,你将不得不每周第一天进行排序:

data = sorted(data, key=itemgetter(-2))

You can tackle this problem with a simple loop instead. 您可以通过简单的循环来解决此问题。 Create a results dict with initial values for each day set to zero, and just add to it step by step. 创建一个结果字典,每天的初始值设置为零,然后逐步添加。

results = {k:0 for k in range(1,8)}
#Output: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}
data = [[2004,1,1,1,50], [2008,2,30,1,150],[1984,5,1,3,20],[1982,5,1,7,20], [1982,5,8,7,20]]

for x in data:
    results[x[3]] += x[4]

print(results)
#Output:
{1: 200, 2: 0, 3: 20, 4: 0, 5: 0, 6: 0, 7: 40}

As you asked to use Counter from collections, you could use it like this: 当您要求从集合中使用Counter时,您可以像这样使用它:

from collections import Counter

counter=Counter()

for group in data:
    counter[group[3]] +=group[4]

results=dict(counter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM