Python Data Structures

8. Data Structures

튜플 (Tuple)

튜플은 변경할 수 없는 순서가 있는 데이터 구조입니다. 여러 데이터 타입을 포함할 수 있으며, 인덱스를 사용하여 요소에 접근할 수 있습니다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 튜플 생성
t = (1, 2, 'three', 4.0)
# 인덱스를 통한 접근
print(t[0]) # 1
# 길이 확인
print(len(t)) # 4
# 튜플 생성 t = (1, 2, 'three', 4.0) # 인덱스를 통한 접근 print(t[0]) # 1 # 길이 확인 print(len(t)) # 4
# 튜플 생성
t = (1, 2, 'three', 4.0)

# 인덱스를 통한 접근
print(t[0])  # 1

# 길이 확인
print(len(t))  # 4

딕셔너리 (Dictionary)

딕셔너리는 키-값 쌍으로 구성된 데이터 구조입니다. 키를 사용하여 값을 참조할 수 있으며, 중복된 키는 허용되지 않습니다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 딕셔너리 생성
person = {
'name': 'Alice',
'age': 30,
'city': 'New York'
}
# 특정 키의 값 접근
print(person['name']) # Alice
# 모든 키 출력
print(person.keys()) # dict_keys(['name', 'age', 'city'])
# 딕셔너리 생성 person = { 'name': 'Alice', 'age': 30, 'city': 'New York' } # 특정 키의 값 접근 print(person['name']) # Alice # 모든 키 출력 print(person.keys()) # dict_keys(['name', 'age', 'city'])
# 딕셔너리 생성
person = {
    'name': 'Alice',
    'age': 30,
    'city': 'New York'
}

# 특정 키의 값 접근
print(person['name'])  # Alice

# 모든 키 출력
print(person.keys())  # dict_keys(['name', 'age', 'city'])

집합 (Set)

집합은 중복 요소가 없고 순서가 없는 데이터 구조입니다. 집합 연산을 통해 합집합, 교집합, 차집합 등의 연산이 가능합니다.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 집합 생성
numbers = {1, 2, 3, 4, 5}
# 요소 추가
numbers.add(6)
# 요소 제거
numbers.remove(3)
# 합집합
other_numbers = {4, 5, 6, 7, 8}
union = numbers | other_numbers
# 교집합
intersection = numbers & other_numbers
print(union) # {1, 2, 4, 5, 6, 7, 8}
print(intersection) # {4, 5, 6}
# 집합 생성 numbers = {1, 2, 3, 4, 5} # 요소 추가 numbers.add(6) # 요소 제거 numbers.remove(3) # 합집합 other_numbers = {4, 5, 6, 7, 8} union = numbers | other_numbers # 교집합 intersection = numbers & other_numbers print(union) # {1, 2, 4, 5, 6, 7, 8} print(intersection) # {4, 5, 6}
# 집합 생성
numbers = {1, 2, 3, 4, 5}

# 요소 추가
numbers.add(6)

# 요소 제거
numbers.remove(3)

# 합집합
other_numbers = {4, 5, 6, 7, 8}
union = numbers | other_numbers

# 교집합
intersection = numbers & other_numbers

print(union)  # {1, 2, 4, 5, 6, 7, 8}
print(intersection)  # {4, 5, 6}

내장 데이터 구조 관련 메서드

각 데이터 구조는 다양한 내장 메서드를 제공하여 데이터를 추가, 삭제, 수정하거나 관련 정보를 조회할 수 있습니다. 이는 데이터를 보다 효율적으로 관리하고 사용할 수 있도록 도와줍니다.

이와 같이 Python의 다양한 데이터 구조를 이해하고 활용하면, 데이터를 효율적으로 관리하고 처리하는 데 큰 도움이 됩니다.

Leave a Reply

Your email address will not be published. Required fields are marked *