python创建字典列表,每个元素以分钟分隔
嘿,伙计们,我有一个字典列表,上面有时间戳,我想在每次迭代中按分钟分开 因此,目前我的字典如下所示:python创建字典列表,每个元素以分钟分隔,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,嘿,伙计们,我有一个字典列表,上面有时间戳,我想在每次迭代中按分钟分开 因此,目前我的字典如下所示: { "taco.tuesday": [ { "timestamp": "2021-02-11 02:18:00-05:00", "cost": 0 }, { "timestamp&
{
"taco.tuesday": [
{
"timestamp": "2021-02-11 02:18:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:19:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:20:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:21:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:22:00-05:00",
"cost": 0
}
],
"macoroni.mondays": [
{
"timestamp": "2021-02-11 02:18:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:19:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:20:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:21:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:22:00-05:00",
"cost": 0
}
],
"salmon.sundays": [
{
"timestamp": "2021-02-11 02:18:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:19:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:20:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:21:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:22:00-05:00",
"cost": 0
}
],
"whatever.wednesday": [
{
"timestamp": "2021-02-11 02:18:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:19:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:20:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:21:00-05:00",
"cost": 0
},
{
"timestamp": "2021-02-11 02:22:00-05:00",
"cost": 0
}
]
}
但我希望我的结果每分钟都是这样的:
[{
"taco.tuesday": {
"timestamp": "2021-02-11 01:38:00-05:00",
"cost": 0
},
"macoroni.mondays": {
"timestamp": "2021-02-11 01:38:00-05:00",
"cost": 0
},
"salmon.sundays": {
"timestamp": "2021-02-11 01:38:00-05:00",
"cost": 0
},
"whatever.wednesday": {
"timestamp": "2021-02-11 01:38:00-05:00",
"cost": 0
}
}],
[{
"taco.tuesday": {
"timestamp": "2021-02-11 01:39:00-05:00",
"cost": 0
},
"macoroni.mondays": {
"timestamp": "2021-02-11 01:39:00-05:00",
"cost": 0
},
"salmon.sundays": {
"timestamp": "2021-02-11 01:39:00-05:00",
"cost": 0
},
"whatever.wednesday": {
"timestamp": "2021-02-11 01:39:00-05:00",
"cost": 0
}
}],
[{
"taco.tuesday": {
"timestamp": "2021-02-11 01:40:00-05:00",
"cost": 0
},
"macoroni.mondays": {
"timestamp": "2021-02-11 01:40:00-05:00",
"cost": 0
},
"salmon.sundays": {
"timestamp": "2021-02-11 01:40:00-05:00",
"cost": 0
},
"whatever.wednesday": {
"timestamp": "2021-02-11 01:40:00-05:00",
"cost": 0
}
}]
以下是我到目前为止为获得我想要的结果所做的工作:
e = defaultdict(dict)
for i in range(len(ts_results)):
for key, value in tables.items():
e[key]['timesamp'] = str(ts_results[i])
e[key]['cost'] = 0
实现此解决方案的最佳方法是什么?无需使用其他库,您可以执行以下操作:
dict_by_time={}
#为了你的口述(这里称为d)
dict_by_time={}
对于d.项()中的k,v:
在第五章中记录:
t=记录[“时间戳”]
如果在指定时间之前不在目录中:
按时间[t]={}
按时间[t]记录。更新({k:record})
这将为您提供一个按时间戳字符串索引的dict,如果您只需要这些值,可以使用:
列表(按时间值()记录)
无需使用其他库,您可以执行以下操作:
dict_by_time={}
#为了你的口述(这里称为d)
dict_by_time={}
对于d.项()中的k,v:
在第五章中记录:
t=记录[“时间戳”]
如果在指定时间之前不在目录中:
按时间[t]={}
按时间[t]记录。更新({k:record})
这将为您提供一个按时间戳字符串索引的dict,如果您只需要这些值,可以使用:
列表(按时间值()记录)
这是基于安格尔·伊瓜拉达的答案,但纠正了他创建嵌套词典的方式
dict_by_time = {}
# for over your dict (called d here)
for k,v in d.items():
for record in v:
t = record["timestamp"]
d = dict_by_time.get(t, {})
d[k] = record
dict_by_time[t] = d
result = list(dict_by_time.values())
这是基于安格尔·伊瓜拉达的答案,但纠正了他创建嵌套词典的方式
dict_by_time = {}
# for over your dict (called d here)
for k,v in d.items():
for record in v:
t = record["timestamp"]
d = dict_by_time.get(t, {})
d[k] = record
dict_by_time[t] = d
result = list(dict_by_time.values())
StackOverflow不是免费的编码服务。你应该会的。请更新您的问题,以显示您已在某个应用程序中尝试过的内容。有关更多信息,请参阅,并选择:)您可能会发现
itertools
库中的函数很有用。@Barmar感谢您的回复。到目前为止,我已经尝试将列表转换为dict,但这不是解决方案,因为它会删除除最后一分钟外的所有其他时间戳TestAckOverflow不是免费的编码服务。你应该会的。请更新您的问题,以显示您已在某个应用程序中尝试过的内容。有关更多信息,请参阅,并选择:)您可能会发现itertools
库中的函数很有用。@Barmar感谢您的回复。到目前为止,我已经尝试将列表转换为dict,但这不是解决方案,因为它会删除除最后一分钟之外的所有其他时间戳。这不会产生期望的结果。dict\u by\u time
的每个元素都是一个字典列表,而不是一个由昵称键入的字典。你说得对,我错过了那部分,现在应该没问题了。这不会产生期望的结果。dict\u by\u time
的每个元素都是一个字典列表,而不是一个由昵称组成的字典。你是对的,我错过了那部分,现在应该可以了