STUDY

python data structures

02:00AM 2022. 8. 30. 17:09

python 자료구조 3가지.
1. list -> 정렬된 리스트(가변)
2. tuple -> 정렬된 리스트(불변)
3. dictionary -> 키: 값 ( key : value)

 

1.List(가변)

name = "min"
print(name.uppper())
print(name.capitalize())

2.tuple(불변)

List와 tuple의 차이점이 뭘까?!
차이점은
튜플은 내용을 바꿀수 없고(불변성을 가짐)
list는 내용을 바꿀수 있음 (list.append() / list.remove() / list.clear() / list.reverse() )

공통점은
1. 인덱스로 값에 접근 가능. list [2] , tuple [2]
2. 리스트 형태로 정렬된 값을 제공.

3.dictionary

반드시 key 값으로 접근(인덱스 접근 불가)

player = {
	'name':'min',
	'age':12,
	'alive':True,
	'fav_food':['🍕','🍔']
}

print(palyer.get("age"))  --> 12

dictionayry에서 value값으로 String, number, tuple, list 를 넣을수 있음.
단 key에는 list를 제외하고 string, number, tuple 지정할수 있음
단 tuple은 내부 개체가 불변해야 가능.

users = {
  "name": "min",
  "age": 12,
  "alive": True,
  "fav_food":("🥩","🍕"),
  "friend":{
    "name":"nico",
    "common":["🛴","✈"]
  }
}

print(users["friend"]["common"])---> ['🛴', '✈']
print(users["fav_food"])        --->('🥩', '🍕')
print(users["fav_food"][0])     --->🥩

users['fav_food'] ="🚕"         --->error
users.pop("alive")              --->True
users["friend"]["common"].append("🚘")--->success

dict.pop("key") || dict.get("key") 차이?!
dictionary.get("key") dictionary내부에 key로 value에 값을 접근및 확인.
dictionary.pop("key") dictionary내부에 key로 value에 값을 접근 dictionary에서 제외시킴.
ex

users = {
  "name": "min",
  "age": 12,
  "alive": True,
  "fav_food":("🥩","🍕"),
  "friend":{
    "name":"nico",
    "common":["🛴","✈"]
  }
}
print(users.pop("age"))  ->12
print(users.get("age"))  ->None
users = {
  "name": "min",
  "age": 12,
  "alive": True,
  "fav_food":("🥩","🍕"),
  "friend":{
    "name":"nico",
    "common":["🛴","✈"]
  }
}

print(users.get("age"))  ->12
print(users.pop("age"))  ->12
print(users) users dictionay에서 age 값이 빠진것을 확인가능.

'STUDY' 카테고리의 다른 글

python  (0) 2022.09.01
python -  (0) 2022.08.30
python loop  (0) 2022.08.30