2014-11-29 2 views

답변

1

값이 키 집합에 있거나 없는지를 테스트하는 것은 매우 간단합니다. in/not in을 사용하면됩니다.

for value in relationship.values(): 
    if value not in relationship: 
     # value is not in the set of keys 
+0

이것은 나에게 unhashable 목록 오류를 준다. –

2

반드시 가장 빠른 방법은 아니지만 간단합니다. 당신이 중복하지 않으려면

producers = [val for val in relationship.values() if val not in relationship] 

또는 설정 이해 :

producers = {val for val in relationship.values() if val not in relationship} 
다음

쉽게 인쇄 할 수 있습니다 (또는 operaitons의 anyother nunber를)

# Make a set of of the values 
prey = set(relationship.values()) 

# Find the intersection of predators and prey (elements that are both keys and values) 
both = prey.intersection(relationship) 
1

지능형리스트를 사용하여 :

이 솔루션은 나중에 인쇄하거나 다른 이유로 인쇄 할 값을 저장하려는 경우에 유용합니다.

관련 문제