Python 如何使用计数器汇总defaultdict中列表中的元组值?
我有一个Python 如何使用计数器汇总defaultdict中列表中的元组值?,python,python-3.x,Python,Python 3.x,我有一个defaultdict,它将值分组为元组列表。我想使用计数器添加每个键的值 因此,问题是如何转换: dict\u项目([ ("第一",("一","七","一","二","一","十二",, ('Key_Two',[('1',13),('1',9),('1',7),('1',12),('1',2),('1',10)]) ]) 为此: [ 'Key_One':计数器({'1':23,'5':1}) 'Key_Two':计数器({'1':53}) ] 使用collections.defaul
defaultdict
,它将值分组为元组列表。我想使用计数器添加每个键的值
因此,问题是如何转换:
dict\u项目([
("第一",("一","七","一","二","一","十二",,
('Key_Two',[('1',13),('1',9),('1',7),('1',12),('1',2),('1',10)])
])
为此:
[
'Key_One':计数器({'1':23,'5':1})
'Key_Two':计数器({'1':53})
]
使用collections.defaultdict
和一个简单的迭代
Ex:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
输出:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
输出:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
使用collections.defaultdict
和一个简单的迭代
Ex:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
输出:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
输出:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
使用计数器和defaultdict。Python 3.6.7
from collections import Counter
from collections import defaultdict
a = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
l = {}
for i in a:
output = defaultdict(int)
for k, v in a[i]:
output[k] += v
l.update({i: Counter(dict(output.items()))})
print(l)
输出:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
使用计数器和defaultdict。Python 3.6.7
from collections import Counter
from collections import defaultdict
a = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
l = {}
for i in a:
output = defaultdict(int)
for k, v in a[i]:
output[k] += v
l.update({i: Counter(dict(output.items()))})
print(l)
输出:
from collections import defaultdict
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = defaultdict(int)
for m, n in v:
temp[m] += n
result[k] = temp
print(result)
{'Key_One': defaultdict(<class 'int'>, {'1': 23, '5': 1}), 'Key_Two': defaultdict(<class 'int'>, {'1': 53})}
from collections import Counter
d = dict([
('Key_One', [('1', 7), ('1', 2), ('1', 2), ('1', 12), ('5', 1)]),
('Key_Two', [('1', 13), ('1', 9), ('1', 7), ('1', 12), ('1', 2), ('1', 10)])
])
result = {}
for k, v in d.items():
temp = Counter()
for m, n in v:
temp.update(Counter({m:n}))
result[k] = temp
print(result)
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}
{'Key_One': Counter({'1': 23, '5': 1}), 'Key_Two': Counter({'1': 53})}