为了将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 :(得分:3)
我的方法:使用默认值为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)
答案 1 :(得分:2)
使用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}
答案 2 :(得分:2)
git push -u origin master
答案 3 :(得分:0)
您可以编写一个函数来检查密钥是否已存在于字典中。这样的事情会起作用:
d = dict()
for x, y in t:
if x in d:
d[x]+=y
else:
d[x] = y
这将为您提供正确的输出:
{'a': 5, 'b': 2, 'c': 3}
答案 4 :(得分:0)
Not found
在口译员中验证
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
答案 5 :(得分:-1)
这是最快的方法:
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)。
答案 6 :(得分:-1)
尝试:
#*#*gudu*#*#