[英]Python - how to format dictionary but keep relative time stamp?
I have a dictionary like below with the key as a date and the items in the dictionary numerical characters with times. 我有一个像下面这样的字典,键为日期,字典中的项目数字为时间。 I'd like to consolidate the lines, and not lose the timestamp (it can be rounded a little, but should still represent hour and minutes is possible). 我想合并这些行,并且不要丢失时间戳(可以将其四舍五入,但仍应表示小时和分钟)。 I'd like to capture from decimal to decimal but I have no idea how to go about this. 我想从十进制到十进制捕获,但我不知道该如何处理。 The dictionary has been parsed from a text file and is contained in a dict offset_dict. 该字典已从文本文件中解析出来,并包含在dict offset_dict中。
Here is the dictionary: 这是字典:
offset_dict = {'2019/07/15': ['. 11:47:24',
'0 11:47:24 ',
'0 11:47:24 ',
'0 11:47:24',
'1 11:47:24 ',
'. 11:47:47',
'0 11:47:47',
'0 11:47:47',
'0 11:47:47',
'4 11:47:48',
'. 11:51:46',
'0 11:51:47',
'0 11:51:47',
'0 11:51:48',
'3 11:51:48'],
'2019/07/16': ['. 06:24:52',
'1 06:24:53',
'0 06:24:53',
'2 06:24:56',
'8 06:24:57',
'0 06:24:57',
'0 06:24:59',
'8 06:24:59',
'0 06:24:59',
'8 06:25:00',
'0 06:25:03',
'. 06:25:04',
'5 06:25:04',
'0 06:25:05',
'. 06:34:19',
'0 06:34:19',
'0 06:34:19',
'5 06:34:35']}
Running this: 运行此:
for key, line in offset_dict.items():
print(key)
print(line)
Will output the dict as 将dict输出为
2019/07/15
{'. 11:47:24', '3 11:51:48', '1 11:47:24 ', '0 11:47:47', '0 11:47:24 ', '0 11:51:47', '. 11:47:47', '0 11:51:48', '4 11:47:48', '. 11:51:46', '0 11:47:24'}
2019/07/16
{'1 06:24:53', '0 06:25:03', '0 06:24:53', '8 06:24:57', '. 06:34:19', '0 06:25:05', '5 06:34:35', '8 06:24:59', '. 06:24:52', '8 06:25:00', '5 06:25:04', '2 06:24:56', '. 06:25:04', '0 06:34:19', '0 06:24:57', '0 06:24:59'}
The format I'd like to see: 我想查看的格式:
2019/07/15
{'.0001 11:47:00', '.0004 11:47:00', '.0003 11:51:00'}
2019/07/16
{'.1028008080 06:24:00' '.50 06:25:00', '.005 06:34:00'}
Not the cleanest solution but it gets close to the desired output as long as order isn't important and using the timestamps don't need to end with 00
. 这不是最干净的解决方案,但是只要顺序不重要,并且使用时间戳不需要以00
结尾,它就会接近所需的输出。 If order of the output is important I will need to change this slightly. 如果输出的顺序很重要,我将需要对此稍作更改。
for date in offset_dict:
times_for_date = []
decimal_time = []
start_time = None
for entry in offset_dict[date]:
start, time = entry.split()
if start == '.':
if start_time:
times_for_date.append((''.join(decimal_time), start_time))
decimal_time = []
start_time = time
decimal_time.append(start)
if decimal_time:
times_for_date.append((''.join(decimal_time), start_time))
output_set = set(f"{decimal_time} {start_time}" for decimal_time, start_time in times_for_date)
print(f"{date} {output_set}")
Ouput: 输出:
2019/07/15 {'.0004 11:47:47', '.0001 11:47:24', '.0003 11:51:46'}
2019/07/16 {'.50 06:25:04', '.005 06:34:19', '.1028008080 06:24:52'}
Using itertools.groupby
and statistics.mean
to compute the average time: 使用itertools.groupby
和statistics.mean
计算平均时间:
offset_dict = {'2019/07/15': ['. 11:47:24',
'0 11:47:24 ',
'0 11:47:24 ',
'0 11:47:24',
'1 11:47:24 ',
'. 11:47:47',
'0 11:47:47',
'0 11:47:47',
'0 11:47:47',
'4 11:47:48',
'. 11:51:46',
'0 11:51:47',
'0 11:51:47',
'0 11:51:48',
'3 11:51:48'],
'2019/07/16': ['. 06:24:52',
'1 06:24:53',
'0 06:24:53',
'2 06:24:56',
'8 06:24:57',
'0 06:24:57',
'0 06:24:59',
'8 06:24:59',
'0 06:24:59',
'8 06:25:00',
'0 06:25:03',
'. 06:25:04',
'5 06:25:04',
'0 06:25:05',
'. 06:34:19',
'0 06:34:19',
'0 06:34:19',
'5 06:34:35']}
from itertools import groupby
from datetime import datetime
from statistics import mean
out = {}
for k, v in offset_dict.items():
out[k] = []
for vv, gg in groupby(v, lambda k, d={'g':0}: (d.update(g=d['g']+1), d['g']) if k.startswith('.') else (None, d['g'])):
l = [*gg]
d = [datetime.strptime(i.split()[1], '%H:%M:%S').timestamp() for i in l]
out[k].append(''.join(i.split()[0] for i in l) + ' ' + datetime.strftime(datetime.fromtimestamp(mean(d)), '%H:%M:00'))
from pprint import pprint
pprint(out)
Prints: 印刷品:
{'2019/07/15': ['.0001 11:47:00', '.0004 11:47:00', '.0003 11:51:00'],
'2019/07/16': ['.1028008080 06:24:00', '.50 06:25:00', '.005 06:34:00']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.