python列表中字典按指定相同的键值对合并到一个列表中
[ {'date':2019-08-17, 'type':'旅游', 'over_im':'12365'},
{'date':2019-08-17, 'type':'娱乐', 'over_im':'11234'},
{'date':2019-08-17, 'type':'工作', 'over_im':'9754'},
{'date':2019-08-17, 'type':'休闲', 'over_im':'8634'},
{'date':2019-08-17, 'type':'生活', 'over_im':'3256'},
{'date':2019-08-18, 'type':'生活', 'over_im':'15342'},
{'date':2019-08-18, 'type':'旅游', 'over_im':'12332'},
{'date':2019-08-18, 'type':'玩乐', 'over_im':'11232'},
{'date':2019-08-18, 'type':'休闲', 'over_im':'10432'},
{'date':2019-08-18, 'type':'生活', 'over_im':'9532'},
{'date':2019-08-18, 'type':'工作', 'over_im':'7632'},
{'date':2019-08-18, 'type':'游戏', 'over_im':'5422'},
{'date':2019-08-19, 'type':'工作', 'over_im':'9942'},
{'date':2019-08-19, 'type':'游戏', 'over_im':'7842'},
{'date':2019-08-19, 'type':'休闲', 'over_im':'6213'},
{'date':2019-08-19, 'type':'生活', 'over_im':'5267'},
{'date':2019-08-19, 'type':'玩乐', 'over_im':'2445'}
...]
] 展开
list_msg = [{'date':'2019-08-17', 'type':'旅游', 'over_im':'12365'},
{'date':'2019-08-17', 'type':'haha', 'over_im':'12365'},
{'date':'2019-08-18', 'type':'娱乐', 'over_im':'11234'},{'date':'2019-08-19', 'type':'生活', 'over_im':'9532'},]
# 去除字典重复的data ,获取数据包含多少天内容
set_mark = {i['date'] for i in list_msg}
# 去除非法命名字符作为列表后缀:
ver_name = [i.replace('-','_') for i in set_mark]
# 给列表排序,确保按照顺序输出变量
ver_name.sort()
# 设置动态命名模板
list_name_template = 'list_data_'
# 创建local对象,准备创建动态变量
createver = locals()
# 循环遍历数据并创建动态列表变量接收
for mark in set_mark:
# 动态创建变量
createver[list_name_template + mark.replace('-','_')] \
= [dict_current for dict_current in list_msg if dict_current['date'] == mark]
for name in ver_name:
print(list_name_template + name + ':' ,end='\t') # 打印自动创建的变量名称,采用tab分隔
exec('print('+list_name_template + name +')') # 打印变量内容(列表)
'''
打印结果
list_data_2019_08_17: [{'date': '2019-08-17', 'type': '旅游', 'over_im': '12365'}, {'date': '2019-08-17', 'type': 'haha', 'over_im': '12365'}]
list_data_2019_08_18: [{'date': '2019-08-18', 'type': '娱乐', 'over_im': '11234'}]
list_data_2019_08_19: [{'date': '2019-08-19', 'type': '生活', 'over_im': '9532'}]
'''
import pandas as pd
arr=[{'date':'2019-08-17', 'type':'旅游', 'over_im':'12365'},{'date':'2019-08-17', 'type':'娱乐', 'over_im':'11234'},{'date':'2019-08-17', 'type':'工作', 'over_im':'9754'},{'date':'2019-08-17', 'type':'休闲', 'over_im':'8634'},{'date':'2019-08-17', 'type':'生活', 'over_im':'3256'},{'date':'2019-08-18', 'type':'生活', 'over_im':'15342'},{'date':'2019-08-18', 'type':'旅游', 'over_im':'12332'},{'date':'2019-08-18', 'type':'玩乐', 'over_im':'11232'},{'date':'2019-08-18', 'type':'休闲', 'over_im':'10432'},{'date':'2019-08-18', 'type':'生活', 'over_im':'9532'},{'date':'2019-08-18', 'type':'工作', 'over_im':'7632'},{'date':'2019-08-18', 'type':'游戏', 'over_im':'5422'},{'date':'2019-08-19', 'type':'工作', 'over_im':'9942'},{'date':'2019-08-19', 'type':'游戏', 'over_im':'7842'},{'date':'2019-08-19', 'type':'休闲', 'over_im':'6213'},{'date':'2019-08-19', 'type':'生活', 'over_im':'5267'},{'date':'2019-08-19', 'type':'玩乐', 'over_im':'2445'}]
df=pd.DataFrame(arr)
print(df.set_index(['date','type']))
请点击输入图片描述
pandas能很方便的处理
大神好:使用pandas处理后的数据该怎么取值呢?
什么叫怎么取值?另存为Excel文件?
列表1作为key,列表2作为value
下面看一下代码:
#定义两个列表
list1 = range(0,10)
list2 = range(10,20)
#合并为字典,调用dict(zip())
dict_name = dict(zip(list1,list2))
print(dict_name)
运行结果:
{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}
感觉不能满足需求,如何能按照date的value来进行分割,将date相同的字典添加到一个新列表中
2019-08-21 · 知道合伙人软件行家
2019-08-23