• 목록
  • 아래로
  • 위로
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

Source code: Lib/collections/__init__.py This module implements specialized container datatypes provi...

 

작성자
이니스프리 119 Lv. (0%) 1973470/115200000EXP

Make StudyForUs Great Again!

 

CSVpuymXAAAVVpd.jpg

댓글 0

권한이 없습니다.