Python: 목록 대 사전
먼저 둘 다에 대한 소개를 참조하십시오.
목록:
파이썬에서
list
는 objects
와 다른 types
의 순서를 저장하는 가장 다재다능한 데이터 구조입니다.Python
list
에는 mutable
가 있습니다. 이는 이러한 요소가 str
또는 tuple
와 달리 변경될 수 있음을 의미합니다. elements
라는 목록의 이들items
.파이썬에서
list
를 생성하는 방법은 여러 가지가 있지만 가장 간단한 방법은 [ ]
를 사용하고 ,
로 구분된 항목을 넣는 것입니다.목록을 만드는 다양한 방법:
a = [] # provide empty list
b = [1,2,3]
c = [x for x in iterable]
d = list('abc')
e = list((1,2,3))
사전:
Python
dictionary
에서 매핑 개체는 hashable
값을 arbitrary
개체에 매핑합니다. Python의 Mapping type
이며 표준mapping
유형은 dictionary
하나뿐입니다. 역시 mutable
입니다. Pythondictionary
에는 key
, value
쌍이 있으며 여기서 keys
는 거의 임의적이며 Hashable
, list
또는 기타 dictionary
유형을 포함하는 mutable
유사 값이 아닌 값은 사용되지 않습니다. keys
의 dictionary
와 같이 .Numeric types used for
keys
obey the normal rules fornumeric comparison
: if two numbers compare equal (such as1
and1.0
) then they can be used interchangeably to index the samedictionary entry
.
lists
와 같이 다양한 방법으로 사전을 만들 수 있지만 가장 간단한 방법은 :
와 keys
의 분리된 쌍을 values
안에 전달하는 것입니다.사전을 만드는 다양한 방법:
a = dict(one=1, two=2, three=3)
b = {'one': 1, 'two': 2, 'three': 3}
c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
d = dict([('two', 2), ('one', 1), ('three', 3)])
e = dict({'three': 3, 'one': 1, 'two': 2})
f = dict({'one': 1, 'three': 3}, two=2)
All the above examples return a dictionary equal to
{"one": 1, "two": 2, "three": 3}
.
이제 우리는 주제에 도달했습니다. 우리는 일반적으로 목록이 사용하기 쉽기 때문에 프로그램에서 목록을 사용하지만 예를 들어
{ }
항목이 엄청나게 많은 경우 어떻게 해야 합니까? 그런 다음 5000000
는 항목을 조회하는 데 많은 시간이 걸리지만 lists
와 동일한 작업을 수행하면 Python에서 dictionary
의 평균 시간 복잡도가 dictionary key lookup
이므로 매우 빠르게 수행됩니다. 해시 테이블로. O(1)
의 시간 복잡도는 평균적으로 lookup in a list
입니다. 그러나 O(n)
와 비교하여 dictionary
및 keys
쌍을 저장하는 데 많은 공간을 차지한다는 점에서 values
를 사용하는 데 또 하나의 문제가 있습니다. 그것이 lists
의 최상의 상황입니다.If you search for a fixed number of keys, if you grow your haystack size (i.e. the size of the collection you’re searching through) by
10,000x
from1k
entries to10M
entries, using adict
is over5000x
faster than using alist
! (Source: Fluent Python by Luciano Ramalho)
그러나
space-time tradeoff
이후에 우리는 python 3.6
이전보다 더 적게 소요되는 dict
의 새로운 구현을 가지므로 이제 더 이상 memory
이 아닙니다.The
dict
type has been reimplemented to use a more compact representation based on a proposal by Raymond Hettinger and similar to thePyPy dict
implementation. This resulted in dictionaries using20% to 25%
less memory when compared toPython 3.5
. (Source : python.org)
읽어 주셔서 감사합니다.
Reference
이 문제에 관하여(Python: 목록 대 사전), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/cdaman123/python-list-vs-dictionaries-3c6f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)