字典求和
edge_weights = defaultdict(lambda: defaultdict(float))
for idx,node in enumerate(graph.nodes()):
node2com[node] = idx #给每一个节点初始化赋值一个团id
for edge in graph[node].items():
edge_weights[node][edge[0]] = edge[1]['weight']
edge_weights
运行结果:
defaultdict(<function __main__.<lambda>()>,
{'397564': defaultdict(float,
{'15.1.18010.11898': 71,
'15.1.18010.11899': 54,
'15.1.18009.11899': 75,
'15.1.18009.11898': 160}),
'15.1.18010.11898': defaultdict(float,
{'397564': 71,
'577806': 61,
'73827465': 66,
'30009791666': 62,
'30005407392': 59,
'100293225': 102,
'30012147301': 65,
'138661946': 52}),
'1085941': defaultdict(float,
{'15.1.18007.11870': 120,
'15.1.18005.11872': 55,
'15.1.18004.11872': 75,
'15.1.18006.11870': 83,
'15.1.18004.11871': 63})
})
对上述edge_weights所有的值汇入列表并求和:
sum(
[weight for start in edge_weights.keys() for end, weight in edge_weights[start].items()]
)
列表剔重并计数
方法1:
统计列表中的重复项出现的次数。
循环遍历出一个可迭代对象中的元素,如果字典没有该元素,那么就让该元素作为字典的键,并将该键赋值为1,如果存在就将该元素对应的值加1.
lists = ['a','a','b',5,6,7,5,'a']
count_dict = dict()
for item in lists:
if item in count_dict:
count_dict[item] += 1
else:
count_dict[item] = 1
方法2:
使用collections.defaultdict(),将default_factory设为int,代码如下:
from collections import defaultdict
#s = 'mississippi'
s = ['a','a','b',5,6,7,5,'a']
d = defaultdict(int)
for k in s:
d[k] += 1
print('\n',d)
获取字典中最大的value
a = {'a':2,'b':3,'c':5,'d':9,'e':4}
print(max(a.values()))
获取字典中出现value最大的key
a = {'a':2,'b':3,'c':5,'d':9,'e':4}
print(max(a,key=a.get))
运行结果:
d
字典对应元素追加
对于列表:
s = [('yellow',1),('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
统计列表字典有两种方法:
方法1:
用dict.setdefault()实现。
代码如下:
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = {}
for k, v in s:
d.setdefault(k,[]).append(v)
a = sorted(d.items())
print(a)
运行结果:
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
方法2;
使用collections.defaultdict(),并使用list作第一个参数,可以很容易将键-值对序列转换为列表字典,
代码如下:
from collections import defaultdict
s = [('yellow',1),('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
a = sorted(d.items())
print(a)
运行结果:
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
当字典中没有的键第一次出现时,default_factory自动为其返回一个空列表,list.append()会将值添加进新列表;再次遇到相同的键时,list.append()将其它值再添加进该列表。这种方法比使用dict.setdefault()更为便捷。
字典对应元素追加并剃重
对于列表:
s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
统计并剃重:
from collections import defaultdict
s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
d = defaultdict(set)
for k, v in s:
d[k].add(v)
print('\n',d)
运行结果:
defaultdict(<class 'set'>, {'red': {1, 3}, 'blue': {2, 4}})
对字典进行过滤
创建一个新的字典,可以利用字典推导式
headerTable = {k: v for k, v in headerTable.items() if v > 2}
反转字典的方法(字典的key和value对换)
使用字典推导:
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
{v: k for k, v in m.items()}
使用压缩器:
m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
m.items() #[('a', 1), ('c', 3), ('b', 2), ('d', 4)]
zip(m.values(), m.keys()) #[(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]
mi = dict(zip(m.values(), m.keys()))
字典的key和value对换并把key按照value进行列表合并
对于字典:
defaultdict(int,
{'2100201919459568780': 0,
'2100201927433498080': 1,
'2100201935997972401': 2,
'2100201934073343294': 3,
'2100201938073398590': 3,
'2100201938426179130': 2,
'2100201938057211020': 4,
'2100201938030472762': 3,
'2100201940356247098': 4,
'2100201939150253460': 4,
'2100201935737728404': 4,
'2100201938984381844': 4,
'2100201937770425806': 4,
'2100201937563397283': 4,
'2100201941426286415': 4,
'2100201936062819790': 4,
'2100201936279351185': 4,
'2100201934074097553': 4,
'2100201940543713169': 4})
进行处理:
track_merge = defaultdict(list)
for i in track_label.items():
track_merge[str(i[1])].append(i[0])
输出:
defaultdict(list,
{'0': ['2100201919459568780'],
'1': ['2100201927433498080'],
'2': ['2100201935997972401', '2100201938426179130'],
'3': ['2100201934073343294',
'2100201938073398590',
'2100201938030472762'],
'4': ['2100201938057211020',
'2100201940356247098',
'2100201939150253460',
'2100201935737728404',
'2100201938984381844',
'2100201937770425806',
'2100201937563397283',
'2100201941426286415',
'2100201936062819790',
'2100201936279351185',
'2100201934074097553',
'2100201940543713169']})
合并字典
appointment = { 'soccer' : { 'day': 20, 'month': 'april' } }
appointment2 = { 'gym' : { 'day': 5, 'month': 'may' } }
appointment.update(appointment2)
appointment
输出:
{
'gym': {'day': 5, 'month': 'may'},
'soccer': {'day': 20, 'month': 'april'}
}
到此这篇关于Python中对字典的几个处理方法分享的文章就介绍到这了,更多相关Python字典处理内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!