개발자를 위한 Python 입문
Jasmin Ludolf
Senior Data Science Content Developer
고유한 데이터 포함
변경 불가
중복을 식별하고 제거하는 것이 이상적
(리스트와 같은 다른 데이터 구조에 비해) 빠른 검색
{}: = 딕셔너리: 없음 = 세트# Create a set of ingredients
ingredients = {"pasta", "tomatoes", "pasta",
"basil", "garlic", "olive oil", "salt"}
print(ingredients)
{'pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'}
# Existing list variable ingredients_list = ["pasta", "tomatoes", "garlic", "basil" "olive oil", "pasta", "salt"]# Convert to a set unique_ingredients = set(ingredients_list)# Check the data type type(unique_ingredients)
set
print(unique_ingredients)
{'pasta', 'tomatoes', 'garlic', 'basil', 'olive oil'}
[]로 부분 집합할 수 없습니다# Trying to subset a set
print(unique_ingredients[0])
TypeError: 'set' object is not subscriptable
ingredients = {"pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"}# Sorting a set print(sorted(ingredients))
['basil', 'garlic', 'olive oil', 'pasta', 'salt', 'tomatoes']
sorted()는 리스트를 반환[0]{{4}})
# Creating a tuple serving_sizes = (1, 2, 4, 6, 8)# Convert another data structure to a tuple ingredients_tuple = tuple(ingredients_list)
# A tuple
serving_sizes = (1, 2, 4, 6, 8)
# Access the second element
print(serving_size[1])
2
개발자를 위한 Python 입문