티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to check if a value exists in a dictionary?

사전에 값이 있는지 확인하는 방법은 무엇입니까?

 문제 내용 

I have the following dictionary in python:

Python에서 다음과 같은 사전이 있습니다:
d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}

 

I need a way to find if a value such as "one" or "two" exists in this dictionary.

이 사전에서 "one" 또는 "two"와 같은 값을 찾아야 합니다.

 

For example, if I wanted to know if the index "1" existed I would simply have to type:

예를 들어, 인덱스 "1"이 존재하는지 알고 싶다면, 다음과 같이 입력할 수 있습니다:
"1" in d

 

And then python would tell me if that is true or false, however I need to do that same exact thing except to find if a value exists.

그러면 파이썬은 그것이 true인지 false인지 알려줍니다. 하지만 나는 값을 찾는 것을 제외하고 동일한 방법으로 작업해야 합니다.

 

 

 

 높은 점수를 받은 Solution 

>>> d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
>>> 'one' in d.values()
True

Out of curiosity, some comparative timing:

호기심에, 몇 가지 비교적인 시간 측정 결과입니다:
>>> T(lambda : 'one' in d.itervalues()).repeat()
[0.28107285499572754, 0.29107213020324707, 0.27941107749938965]
>>> T(lambda : 'one' in d.values()).repeat()
[0.38303399085998535, 0.37257885932922363, 0.37096405029296875]
>>> T(lambda : 'one' in d.viewvalues()).repeat()
[0.32004380226135254, 0.31716084480285645, 0.3171098232269287]

 

EDIT: And in case you wonder why... the reason is that each of the above returns a different type of object, which may or may not be well suited for lookup operations:

편집: 그리고 왜 그런지 궁금하다면... 위의 각 결과는 조회 작업에 적합한 유형의 객체를 반환하지만, 반환되는 객체 유형이 서로 다릅니다.
>>> type(d.viewvalues())
<type 'dict_values'>
>>> type(d.values())
<type 'list'>
>>> type(d.itervalues())
<type 'dictionary-valueiterator'>

 

EDIT2: As per request in comments...

편집2: 댓글에서 요청한 것처럼...
>>> T(lambda : 'four' in d.itervalues()).repeat()
[0.41178202629089355, 0.3959040641784668, 0.3970959186553955]
>>> T(lambda : 'four' in d.values()).repeat()
[0.4631338119506836, 0.43541407585144043, 0.4359898567199707]
>>> T(lambda : 'four' in d.viewvalues()).repeat()
[0.43414998054504395, 0.4213531017303467, 0.41684913635253906]

 

 

 가장 최근 달린 Solution 

Different types to check the values exists

다른 방법으로는 값이 존재하는지 확인하는 것이 있습니다.
d = {"key1":"value1", "key2":"value2"}
"value10" in d.values() 
>> False

 

What if list of values

만약 값 목록이 있다면
test = {'key1': ['value4', 'value5', 'value6'], 'key2': ['value9'], 'key3': ['value6']}
"value4" in [x for v in test.values() for x in v]
>>True

 

What if list of values with string values

만약 문자열 값이 포함된 값 목록이라면
test = {'key1': ['value4', 'value5', 'value6'], 'key2': ['value9'], 'key3': ['value6'], 'key5':'value10'}
values = test.values()
"value10" in [x for v in test.values() for x in v] or 'value10' in values
>>True

 

 

출처 : https://stackoverflow.com/questions/8214932/how-to-check-if-a-value-exists-in-a-dictionary

반응형
댓글
공지사항
최근에 올라온 글