Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于特定值连接的Python词典列表_Python_List_Dictionary - Fatal编程技术网

基于特定值连接的Python词典列表

基于特定值连接的Python词典列表,python,list,dictionary,Python,List,Dictionary,我确实有一本字典的目录 list_dict=[{'date':'24-01-2017','amount':12.0},{'date':'24-01-2017','amount':23.0}, {'date':'24-01-2017','amount':2.0},{'date':'25-01-2017','amount':10.0},{'date':'25-01-2017','amount':3.0}, {'date':'26-01-2017','amount':2.0}] 我需要像这样的输出

我确实有一本字典的目录

list_dict=[{'date':'24-01-2017','amount':12.0},{'date':'24-01-2017','amount':23.0},
{'date':'24-01-2017','amount':2.0},{'date':'25-01-2017','amount':10.0},{'date':'25-01-2017','amount':3.0},
{'date':'26-01-2017','amount':2.0}]
我需要像这样的输出

new_dict=[{'total':27.0,[{'date':'24-01-2017','amount':12.0},{'date':'24-01-2017','amount':23.0},
{'date':'24-01-2017','amount':2.0},],[{'total':13.0,{'date':'25-01-2017','amount':10.0},{'date':'25-01-2017','amount':3.0}],[{'total':2.0,
{'date':'26-01-2017','amount':2.0}]]

如何在python中执行此操作?

您的输出不正确,因为您需要一个包含列表的dict,但您可以有如下内容:

dates = list(set([item['date'] for item in list_dict]))
result = [[{'total': sum([item['amount'] for item in list_dict if item['date'] == date])}, [{k: v} for value in list_dict for k, v in value.items() if value['date'] == date]] for date in dates]
[{'total': 13.0}, [{'date': '25-01-2017'}, {'amount': 10.0}, {'date': '25-01-2017'}, {'amount': 3.0}]]
[{'total': 37.0}, [{'date': '24-01-2017'}, {'amount': 12.0}, {'date': '24-01-2017'}, {'amount': 23.0}, {'date': '24-01-2017'}, {'amount': 2.0}]]
[{'total': 2.0}, [{'date': '26-01-2017'}, {'amount': 2.0}]]
结果如下所示:

dates = list(set([item['date'] for item in list_dict]))
result = [[{'total': sum([item['amount'] for item in list_dict if item['date'] == date])}, [{k: v} for value in list_dict for k, v in value.items() if value['date'] == date]] for date in dates]
[{'total': 13.0}, [{'date': '25-01-2017'}, {'amount': 10.0}, {'date': '25-01-2017'}, {'amount': 3.0}]]
[{'total': 37.0}, [{'date': '24-01-2017'}, {'amount': 12.0}, {'date': '24-01-2017'}, {'amount': 23.0}, {'date': '24-01-2017'}, {'amount': 2.0}]]
[{'total': 2.0}, [{'date': '26-01-2017'}, {'amount': 2.0}]]

请更明确地说明您想要实现的目标,并向我们展示您的目标。那么,您可以使用熊猫吗?它看起来是适合这个的工具