파이썬 S60을 사용하고 있습니다 :
stat2 = {}
for s in stat:
for c in s.values():
for k, v in c.items():
if k in stat2:
stat2[k] += v
else:
stat2[k] = v # perhaps copying here
stat2
{'asian': ['japan', 'china'],
'europe': ['germany', 'england', 'french', 'netherland']}
당신은 파이썬> 2.4을 사용한다면, 당신은 단지 defaultdict를 사용할 수 있습니다 :
from collections import defaultdict
stat2 = defaultdict(list)
for s in stat:
for c in s.values():
for k, v in c.items():
stat2[k] += v
stat2
defaultdict(<type 'list'>, {'europe': ['germany', 'england', 'french', 'netherland'], 'asian': ['japan', 'china']})
그리고 정말로 원한다면 :
{'state': [dict(stat2)]}
{'state': [{'asian': ['japan', 'china'],
'europe': ['germany', 'england', 'french', 'netherland']}]}
2001 년에 출시 된 Python 버전 업그레이드를 고려하십시오 ...!
파이썬 2.2? 확실하게 업그레이드 할 수 있습니다 ... – TerryA
여러 항목이있는 하나의 dict 대신 단일 항목 dicts 목록을 원하십니까? 외부 제약 조건이 없다면 이는 다소 이상한 데이터 구조입니다. –