티스토리 뷰

반응형

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

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

 

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

How can I get list of values from dict?

딕셔너리에서 값 리스트를 가져오려면 어떻게 해야 하나요?

 문제 내용 

How can I get a list of the values in a dict in Python?

파이썬에서 딕셔너리의 값 리스트를 얻으려면 어떻게 해야 하나요?

 

In Java, getting the values of a Map as a List is as easy as doing list = map.values();. I'm wondering if there is a similarly simple way in Python to get a list of values from a dict.

Java에서 Map의 값을 List로 가져오는 것은 list = map.values();를 수행하는 것만큼 쉽습니다. Python에서 딕셔너리 값 목록을 가져오는 비슷한 간단한 방법이 있는지 궁금합니다.

 

 

 

 높은 점수를 받은 Solution 

dict.values returns a view of the dictionary's values, so you have to wrap it in list:

dict.values는 딕셔너리 값의 View를 반환하므로 리스트로 묶어야 합니다:
list(d.values())

 

 

 가장 최근 달린 Solution 

There should be one ‒ and preferably only one ‒ obvious way to do it.

그것을 하는 데는 하나의 분명한 방법이 있어야 하고, 가급적이면 하나의 분명한 방법만이 있어야 합니다.

 

Therefore list(dictionary.values()) is the one way.

따라서 list(dictionary.values())가 유일한 방법입니다.

 

Yet, considering Python3, what is quicker?

하지만, 파이썬3를 고려하면, 무엇이 더 빠릅니까?

 

[*L] vs. [].extend(L) vs. list(L)

small_ds = {x: str(x+42) for x in range(10)}
small_df = {x: float(x+42) for x in range(10)}

print('Small Dict(str)')
%timeit [*small_ds.values()]
%timeit [].extend(small_ds.values())
%timeit list(small_ds.values())

print('Small Dict(float)')
%timeit [*small_df.values()]
%timeit [].extend(small_df.values())
%timeit list(small_df.values())

big_ds = {x: str(x+42) for x in range(1000000)}
big_df = {x: float(x+42) for x in range(1000000)}

print('Big Dict(str)')
%timeit [*big_ds.values()]
%timeit [].extend(big_ds.values())
%timeit list(big_ds.values())

print('Big Dict(float)')
%timeit [*big_df.values()]
%timeit [].extend(big_df.values())
%timeit list(big_df.values())
Small Dict(str)
256 ns ± 3.37 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
338 ns ± 0.807 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
336 ns ± 1.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Small Dict(float)
268 ns ± 0.297 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
343 ns ± 15.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
336 ns ± 0.68 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Big Dict(str)
17.5 ms ± 142 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
16.5 ms ± 338 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
16.2 ms ± 19.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Big Dict(float)
13.2 ms ± 41 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
13.1 ms ± 919 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
12.8 ms ± 578 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

 

Done on Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz.

1.90GHz에서 Intel(R) Core(TM) i7-8650U CPU에서 수행되었습니다.
# Name                    Version                   Build
ipython                   7.5.0            py37h24bf2e0_0

 

The result

결과는

 

  1. For small dictionaries * operator is quicker
  2. For big dictionaries where it matters list() is maybe slightly quicker
1. 작은 사전의 경우 * 연산자가 더 빠릅니다
2. 중요한 큰 사전의 경우 list()이 약간 빠를 수 있습니다

 

 

 

출처 : https://stackoverflow.com/questions/16228248/how-can-i-get-list-of-values-from-dict

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