Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python의 내장 데이터 구조

<시간/>

이 기사에서는 Python의 4가지 내장 데이터 구조, 즉 Lists, Dictionaries, Tuples &Sets에 대해 알아볼 것입니다.

목록

목록은 요소의 순서가 지정된 시퀀스입니다. 비 스칼라 데이터 구조이며 본질적으로 변경 가능합니다. 동일한 데이터 유형에 속하는 요소를 저장하는 배열과 달리 목록에는 고유한 데이터 유형이 포함될 수 있습니다.

색인을 대괄호로 묶어 목록에 액세스할 수 있습니다.

이제 목록을 더 잘 이해할 수 있도록 그림을 살펴보겠습니다.

lis=['tutorialspoint',786,34.56,2+3j]
# displaying element of list
for i in lis:
   print(i)
# adding an elements at end of list
lis.append('python')
#displaying the length of the list
print("length os list is:",len(lis))
# removing an element from the list
lis.pop()
print(lis)
에서 요소 제거

출력

tutorialspoint
786
34.56
(2+3j)
length os list is: 5
['tutorialspoint', 786, 34.56, (2+3j)]

튜플

또한 Python에서 정의된 비 스칼라 유형입니다. 목록과 마찬가지로 순서가 지정된 문자 시퀀스이기도 하지만 튜플은 본질적으로 변경할 수 없습니다. 이것은 이 데이터 구조로 어떠한 수정도 허용되지 않는다는 것을 의미합니다.

요소는 괄호 안에 쉼표로 구분된 이질적이거나 동질적인 속성일 수 있습니다.

예를 살펴보겠습니다.

예시

tup=('tutorialspoint',786,34.56,2+3j)
# displaying element of list
for i in tup:
   print(i)

# adding elements at the end of the tuple will give an error
# tup.append('python')

# displaying the length of the list
print("length os tuple is:",len(tup))

# removing an element from the tup will give an error
# tup.pop()

출력

tutorialspoint
786
34.56
(2+3j)
length os tuple is: 4

세트

중복이 없는 정렬되지 않은 개체 모음입니다. 이것은 모든 요소를 ​​중괄호로 묶어서 수행할 수 있습니다. "set"이라는 키워드를 통해 유형 캐스팅을 사용하여 집합을 형성할 수도 있습니다.

세트의 요소는 변경할 수 없는 데이터 유형이어야 합니다. Set은 인덱싱, 슬라이싱, 연결 및 복제를 지원하지 않습니다. 인덱스를 사용하여 요소를 반복할 수 있습니다.

이제 예를 살펴보겠습니다.

set_={'tutorial','point','python'}
for i in set_:
   print(i,end=" ")
# print the maximum and minimum
print(max(set_))
print(min(set_))
# print the length of set
print(len(set_))

출력

tutorial point python tutorial
point
3

사전

사전은 키-값 쌍의 순서가 지정되지 않은 시퀀스입니다. 인덱스는 변경할 수 없는 모든 유형이 될 수 있으며 키라고 합니다. 이것은 중괄호 안에도 지정됩니다.

연관된 고유 키를 사용하여 값에 액세스할 수 있습니다.

예를 들어 보겠습니다.

# Create a new dictionary
d = dict()

# Add a key - value pairs to dictionary
d['tutorial'] = 786
d['point'] = 56

# print the min and max
print (min(d),max(d))
# print only the keys
print (d.keys())
# print only values
print (d.values())

출력

point tutorial
dict_keys(['tutorial', 'point'])
dict_values([786, 56])

결론

이 기사에서 우리는 Python 언어에 있는 내장 데이터 구조와 그 구현에 대해 배웠습니다.