从单个列表创建字典字典 - Python3

Linux 上的 Python 3.6.5/3.7.1


努力创建一个以字典为值的字典。


我想从列表日期和时间数据创建一个字典(最终创建带有散景的图表)。


以前肯定有人问过这个问题,但我找不到一组搜索词来返回对我来说澄清问题的结果。


nb 我本质上是一个爱好编码器,并且我不容易像真正的程序员一样在算法上思考。


数据在一个列表中(最多 3200 个项目):每个项目是一个日期在一个小时的时钟周期内发生的事件的记录。


因此; ['03/01/19 09:00', '03/01/19 09:00', '03/01/19 09:00',]表示 03/01/2019 0900-1000 之间的 3 个事件。


只记录带有事件的时钟周期,因此如果没有事件,则没有时间戳。


nb 日期格式是 ddmmyy


示例数据:


dtl = [

    '06/01/19 12:00', '06/01/19 12:00', '06/01/19 11:00', '05/01/19 21:00',

    '05/01/19 17:00', '05/01/19 17:00', '05/01/19 14:00', '03/01/19 21:00',

    '03/01/19 17:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00',

    '03/01/19 12:00', '03/01/19 12:00', '03/01/19 11:00', '03/01/19 10:00',

    '03/01/19 10:00', '03/01/19 09:00','03/01/19 09:00','03/01/19 09:00',

]

所需的字典如下所示:


dtd = {

    '03/01/19': {

         '00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0,

         '06': 0, '07': 0, '08': 0, '09': 3, '10': 2, '11': 1,

         '12': 5, '13': 0, '14': 0, '15': 0, '16': 0, '17': 1,

         '18': 0, '19': 0, '20': 0, '21': 1, '22': 0, '23': 0,

     },

     '04/01/19': {

         '00': 0, ... '23': 0

     },

     '05/01/19': {

         '00': 0, ... 

     } ... etc

}

显然,我至少可以用以下键初始化字典:


{i.split()[0]:{} for i in dtl}

但是后来我无法理解我需要做什么来更新带有计数的 subdicts,因此看不到从原始列表到所需字典的方法。我在兜兜转转!


慕雪6442864
浏览 201回答 3
3回答

潇潇雨雨

一旦按日期拆分为字典,就可以将 aCounter与 a结合起来defaultdict非常有效地执行此操作。所以首先按日期拆分:from collections import Counter, defaultdictdtd = defaultdict(list)for date, time in (item.split() for item in dtl):    dtd[date].append(time[:2])现在您可以轻松地计算现有项目,并使用它们来初始化一个defaultdict将在缺失时间返回零的值:for key in dtd:    dtd[key] = defaultdict(int, Counter(dtd[key]))结果是:defaultdict(list, {    '03/01/19': defaultdict(int, {        '09': 3,        '10': 2,        '11': 1,        '12': 5,        '17': 1,        '21': 1    }),    '05/01/19': defaultdict(int, {'14': 1, '17': 2, '21': 1}),    '06/01/19': defaultdict(int, {'11': 1, '12': 2})})由于这里的对象是defaultdicts,您将能够查询不在原始数据集中的日期和时间。您可以通过dict在完成后将结果转换为仅包含您想要的键的正则来避免这种情况:hours = ['%02d' % h for h in range(24)]dtd = {date: {h: d[h] for h in hours} for date, d in dtd}

慕尼黑5688855

我建议使用,collections.defaultdict因为您的某些计数可能为 0。这是一个选项:from collections import defaultdictdtl = ['06/01/19 12:00', '06/01/19 12:00', '06/01/19 11:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'05/01/19 21:00', '05/01/19 17:00', '05/01/19 17:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'05/01/19 14:00', '03/01/19 21:00', '03/01/19 17:00',&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 12:00', '03/01/19 12:00', '03/01/19 11:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 10:00', '03/01/19 10:00', '03/01/19 09:00',&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 09:00','03/01/19 09:00',]# Nested defaultdictresult = defaultdict(lambda: defaultdict(int))for date_time in dtl:&nbsp; &nbsp; date, time = date_time.split()&nbsp; &nbsp; result[date][time.split(':')[0]] += 1输出(使用pprint):defaultdict(<function <lambda> at 0x7f20d5c37c80>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {'03/01/19': defaultdict(<class 'int'>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{'09': 3,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '10': 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '11': 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '12': 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '17': 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '21': 1}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'05/01/19': defaultdict(<class 'int'>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{'14': 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '17': 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '21': 1}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'06/01/19': defaultdict(<class 'int'>, {'12': 2, '11': 1})})如果您真的想显示0for 打印,那么我真的看不到times像我在这里所做的那样保留数组并以dict这种方式初始化您的方法。times = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'22', '23']dtl = ['06/01/19 12:00', '06/01/19 12:00', '06/01/19 11:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'05/01/19 21:00', '05/01/19 17:00', '05/01/19 17:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'05/01/19 14:00', '03/01/19 21:00', '03/01/19 17:00',&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 12:00', '03/01/19 12:00', '03/01/19 11:00',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 10:00', '03/01/19 10:00', '03/01/19 09:00',&nbsp; &nbsp; &nbsp; &nbsp;'03/01/19 09:00','03/01/19 09:00']result = {date_time.split()[0] : {time : 0 for time in times} for date_time in dtl}for date_time in dtl:&nbsp; &nbsp; date, time = date_time.split()&nbsp; &nbsp; result[date][time.split(':')[0]] += 1输出如下:{'06/01/19': {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0, '06': 0, '07': 0, '08': 0, '09': 0, '10': 0, '11': 1, '12': 2, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, '20': 0, '21': 0, '22': 0, '23': 0}, '05/01/19': {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0, '06': 0, '07': 0, '08': 0, '09': 0, '10': 0, '11': 0, '12': 0, '13': 0, '14': 1, '15': 0, '16': 0, '17': 2, '18': 0, '19': 0, '20': 0, '21': 1, '22': 0, '23': 0}, '03/01/19': {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0, '06': 0, '07': 0, '08': 0, '09': 3, '10': 2, '11': 1, '12': 5, '13': 0, '14': 0, '15': 0, '16': 0, '17': 1, '18': 0, '19': 0, '20': 0, '21': 1, '22': 0, '23': 0}}

素胚勾勒不出你

一种快速而肮脏的方法是:#!/usr/bin/env python3def convert(dt):&nbsp; &nbsp; ret = {}&nbsp; &nbsp; for elem in dt:&nbsp; &nbsp; &nbsp; &nbsp; d,t = elem.split()&nbsp; &nbsp; &nbsp; &nbsp; t = t.split(":")[0]&nbsp; &nbsp; &nbsp; &nbsp; # not a valid value&nbsp; &nbsp; &nbsp; &nbsp; if not d: pass&nbsp; &nbsp; &nbsp; &nbsp; # we inserted d already&nbsp; &nbsp; &nbsp; &nbsp; if d in ret:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if t in ret[d]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret[d][t] += 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ret[d] = {'00': 0, '01': 0, '02': 0, '03': 0, '04': 0, '05': 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '06': 0, '07': 0, '08': 0, '09': 0, '10': 0, '11': 0,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '18': 0, '19': 0, '20': 0, '21': 0, '22': 0, '23': 0 }&nbsp; &nbsp; return retdtl = ['06/01/19 12:00', '06/01/19 12:00', '06/01/19 11:00', '05/01/19 21:00', '05/01/19 17:00', '05/01/19 17:00', '05/01/19 14:00', '03/01/19 21:00', '03/01/19 17:00','03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 12:00', '03/01/19 11:00', '03/01/19 10:00', '03/01/19 10:00', '03/01/19 09:00','03/01/19 09:00','03/01/19 09:00']print(convert(dtl))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python