2010-02-11 7 views
2

저는 사전에있는 모든 값들의 목록을 출력하는 함수를 만들고 싶습니다. 목록에는 이중 항목이 없어야합니다. 목록은 사전 순이어야합니다. 필자는 Python에 익숙하지 않고, iteritems() 함수를 사용하여 사전의 모든 값을 출력하는 것보다 더 나아갈 수 없습니다.중첩 된 사전에서 Python으로 고유 값을 추출하는 방법은 무엇입니까?

사전은 다음과 같습니다

critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 
'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 
'The Night Listener': 3.0}, 
'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5, 
'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0, 
'You, Me and Dupree': 3.5}, 
'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0, 
'Superman Returns': 3.5, 'The Night Listener': 4.0}, 
'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 
'The Night Listener': 4.5, 'Superman Returns': 4.0, 
'You, Me and Dupree': 2.5}, 
'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 
'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0, 
'You, Me and Dupree': 2.0}, 
'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0, 
'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5}, 
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}} 

그래서 내가 평가 된 영화의 목록을 인쇄 할. 좋아요 : 그냥 내 행운; 레이디 물 속에; 평면에 뱀; 수퍼맨 반품; 당신, 나와 듀프리; . . . 기타 ...

아무도 도와 줄 수 있습니까?

답변

4

가장 간단한 방법은 다음과 같습니다

>>> d = {1: 'sadf', 2: 'sadf', 3: 'asdf'} 
>>> sorted(set(d.itervalues())) 
['asdf', 'sadf'] 

인쇄를 원하는대로. 당신의 업데이트 질문 답변

은 다음과 같습니다

>>> films = set() 
>>> _ = [films.update(dic) for dic in critics.itervalues()] 
>>> sorted(films) 
['Just My Luck', 'Lady in the Water', 'Snakes on a Plane', 'Superman Returns', 'The Night Listener', 'You, Me and Dupree'] 
+0

많은 감사합니다 : D – Alphonse

0

또 다른 해결 방법 : 답변

>>> reduce(lambda x,y: set(x) | set(y),[ y.keys() for y in critics.values() ]) 
set(['Lady in the Water', 'Snakes on a Plane', 'You, Me and Dupree', 'Just My Luck', 'Superman Returns', 'The Night Listener']) 
관련 문제