- 0
- 이니스프리
- 조회 수 603
1 2 3 4 5 6 | from collections import Counter dict1 = { 'foo1' : 1 , 'foo2' : 3 , 'foo3' : - 5 } dict2 = { 'foo1' : 10 , 'foo2' : - 1 , 'foo3' : 20 } avg = {k: round ((v / 2 ), 2 ) for k, v in sorted ((Counter(dict1) + Counter(dict2)).items(), key = lambda x: - x[ 1 ])} |
결과 : avg = {'foo3': 7.5, 'foo1' : 5.5, 'foo2' : 1}
두 개의 dictionary를 합치거나 빼는 것은 collections.Counter 클래스를 사용하여 할 수 있습니다.
https://docs.python.org/3.12/library/collections.html#collections.Counter
작성자
댓글 0
권한이 없습니다.