我是python的新手并使用词典和列表。 这是清单
detail = [(1, [u'apple', u'2017-07-03T08:03:32Z', 'boston']),
(2, [u'orange', u'2017-07-03T08:58:35Z', 'NOLOCATION']),
(3, [u'grape', u'2017-07-03T12:14:12Z', 'boston']),
(4, [u'cherry', u'2017-07-04T13:16:44Z', 'new york']),
(5, [u'strawberry', u'2017-07-06T10:56:22Z', 'san francisco']),
(6, [u'plum', u'2017-07-06T10:56:22Z', 'seattle'])]
我想对此进行总结,以便 - 对于每个日期,我会分配每个位置的计数。像这样的东西 -
details_summary = {'2017-07-03':[(boston,2), (NOLOCATION,1)], '2017-07-04':
[new york,1], '2017-07-06':[(san francisco,1),(seattle,1)]}
我想要这种格式,因为我想为每个日期(键)和位置点(值)生成地图(可视化)。
我最终创建了两个不同的词典 -
location = {u'boston': 2, 'NOLOCATION': 1, u'new york': 1, u'san francisco':
1, u'seattle': 1}
date = {'2017-07-03':3, '2017-07-04':1, '2017-07-06':2}
现在,我想总结一下,以便我得到,在每个日期的不同地点分开计数,我被困在这里。
答案 0 :(得分:3)
from collections import Counter
d = {}
for k, (w, t, l) in detail:
date = t.split('T')[0] # you can choose to enhance date "isolation"
if date in d:
d[date].append(l)
else:
d[date] = [l]
details_summary = {k: Counter(d[k]).items() for k in d.keys()}
答案 1 :(得分:1)
使用Python集合defaultdict
和Counter
from collections import defaultdict, Counter
summary = defaultdict(list)
for item in detail:
summary[item[1][1].split('T')[0]].append(item[1][2])
details_summary = {str(k):[(x,y) for x,y in Counter(v).iteritems()] for k,v in summary.iteritems()}
print details_summary
{'2017-07-06': [('san francisco', 1), ('seattle', 1)], '2017-07-04': [('new york', 1)], '2017-07-03': [('boston', 2), ('NOLOCATION', 1)]}