[英]How I can convert a Python Tuple into Dictionary
为了将t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
为dict,我希望输出为{'a':5,'b':2,'c':3}
代替{'a':4,'b':2,'c':3}
。 有没有一种方法可以将相同关键项的值相加?
我正在使用此行来转换元组:
dict((x,y) for x, y in t)
我的方法:使用默认值为0的get
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
d = dict()
for x,y in t:
d[x] = d.get(x, 0) + y;
print(d)
使用itertools.groupby
:
from itertools import groupby
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
f = lambda x: x[0]
print({k: sum(x[1] for x in g) for k, g in groupby(sorted(t, key=f), key=f)})
# {'a': 5, 'b': 2, 'c': 3}
或collections.defaultdict
:
from collections import defaultdict
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
d = defaultdict(int)
for x in t:
d[x[0]] += x[1]
print(d)
# {'a': 5, 'b': 2, 'c': 3}
from collections import Counter
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
h = Counter()
for k, v in t:
h[k] += v
h
Counter({'a': 5, 'c': 3, 'b': 2})
您可以编写一个函数来检查密钥是否已存在于字典中。 这样的事情会起作用:
d = dict()
for x, y in t:
if x in d:
d[x]+=y
else:
d[x] = y
这将为您提供正确的输出:
{'a': 5, 'b': 2, 'c': 3}
这几乎完全是collections.defaultdict中的示例
from collections import defaultdict
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
char_to_int = defaultdict(int)
for k, v in t:
char_to_int[k] += v
经过口译员验证
In [1]: from collections import defaultdict
...:
...: t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
...: char_to_int = defaultdict(int)
...:
...: for k, v in t:
...: char_to_int[k] += v
...:
In [2]: char_to_int
Out[2]: defaultdict(int, {'a': 5, 'b': 2, 'c': 3})
这是最快的方法:
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
out = {}
for x, y in t:
if x in out:out[x] += y
else:out[x] = y
输出:
{'a': 5, 'b': 2, 'c': 3}
这是最简单的方法:
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
d = dict.fromkeys(dict(t), 0)
for x, y in t: d[x] += y
输出:
{'a': 5, 'b': 2, 'c': 3}
我使用timeit
来计算用户回答的每种方法花费多少时间。 最快的是我的(方法1):
from timeit import timeit
from itertools import groupby #for method 1
from collections import defaultdict #for method 2 and 5
from collections import Counter #for method 3
t = [('a', 1), ('b', 2), ('c', 3), ('a', 4)]
###############################
######### METHODS ###########
###############################
def method1(): #By Austin
f = lambda x: x[0]
{k: sum(x[1] for x in g) for k, g in groupby(sorted(t, key=f), key=f)}
###############################
def method2(): #By Austin
d = defaultdict(int)
for x in t:
d[x[0]] += x[1]
###############################
def method3(): #By Jean-Claude Arbaut
h = Counter()
for k, v in t:
h[k] += v
###############################
def method4(): #By PHP
d = dict()
for x,y in t:
d[x] = d.get(x, 0) + y;
###############################
def method5(): #By Skam
char_to_int = defaultdict(int)
for k, v in t:
char_to_int[k] += v
###############################
def method6(): #By Aquiles Carattino
d = dict()
for x, y in t:
if x in d:
d[x]+=y
else:
d[x] = y
###############################
def method7(): #My method 2
d = dict.fromkeys(dict(t), 0)
for x, y in t: d[x] += y
###############################
def method8(): #My method 1
out = {}
for x, y in t:
if x in out:out[x] += y
else:out[x] = y
print(timeit(method1))
print(timeit(method2))
print(timeit(method3))
print(timeit(method4))
print(timeit(method5))
print(timeit(method6))
print(timeit(method7))
print(timeit(method8))
我平台上的输出是:
5.7539954 1.7488919999999997 4.2322991000000005 1.0104673999999996 1.3571206 0.8095216999999995 1.5506056999999984 0.6387891000000003
因此,最快的方法是我回答的方法8 (方法#1)。
尝试:
>>> t = ((1, 'a'),(2, 'b')) >>> dict((y, x) for x, y in t) {'a': 1, 'b': 2}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.