List, Tuple, Dictionary

gyomni·2021년 12월 17일
0

Python

목록 보기
7/12
post-thumbnail

List

=> 순서 o, 중복 o, 수정 o, 삭제 o

# 선언

x = []
y = list()
z = [
    1,
    2,
    3,
    4,
]
r = [10, 100, "pen", "banana", "apple"]
q = [10, 100, ["pen", "banana", "apple"]]

# 인덱싱

print(r[3]) # banana
print(r[-2]) # banana
print(r[0] + r[1]) # 110
print(q[2][1]) # banana
print(q[-1][-2]) /# banana

# 슬라이싱

print(r[0:1]) # [10]
print(q[2][1:3]) # ['banana', 'apple']

# 연산

print(z + r) # [1, 2, 3, 4, 10, 100, 'pen', 'banana', 'apple']
print(str(z[0]) + "hi") # 1hi

# List 수정, 삭제

z[0] = 77
print(z) # [77, 2, 3, 4]

z[1:2] = [100, 1000, 10000]
print(z) # [77, 100, 1000, 10000, 3, 4]
z[1] = ["a", "b", "c"]
print(z) # [77, ['a', 'b', 'c'], 1000, 10000, 3, 4]

del z[1] 
print(z) # [77, 1000, 10000, 3, 4]
del z[-1]
print(z) # [77, 1000, 10000, 3]

# List 함수

w = [5, 4, 3, 1, 4]
print(w) # [5, 4, 3, 1, 4]
w.append(6)
print(w) # [5, 4, 3, 1, 4, 6]  ( append() :뒤에 추가 )
w.sort()
print(w) # [1, 3, 4, 4, 5, 6] ( sort() :정렬)
w.reverse()
print(w) # [6, 5, 4, 4, 3, 1] ( reverse() :반대로 (내림차순 x, 리스트 순서의 반대) )
w.insert(2, 7)
print(w) # [6, 5, 7, 4, 4, 3, 1] ( insert(x, y) :x번 인덱스에 y가 들어감. 4 -> 7)
w.remove(4) 
print(w) # [6, 5, 7, 4, 3, 1] ( remove() :리스트 안에서 해당 값 찾아 지워줌. ( 중복되는 모든 값 지워주지 않고. 맨 앞에 하나만 지움.) )
del w[2]  
print(w) # [6, 5, 4, 3, 1] (del :해당 인덱스를 지워줌.)
w.pop()  
print(w) # [6, 5, 4, 3] ( pop() :맨마지막을 꺼내서 없앰. #LIFO (last in first out))
ex = [88, 77]
w.extend(ex)  
print(w) # [6, 5, 4, 3, 88, 77] ( extend() : 리스트 자체가 추가. -> 88,77이 원소로 추가 됨.)

- 삭제 : del, remove, pop


Tuple

=> 순서 o, 중복 o, 수정 x, 삭제x (중요 데이터는 튜플에 저장)

a = ()
b = (1,)
c = (1, 2, 3, 4)
d = (100, 200, ("a", "b", "c"))

* 인덱스를 가지므로 특정 위치의 값을 출력할 수 있음.

print(c[2]) # 3
print(c[3]) # 4
print(d[2][2]) # c

print(d[2:]) # (('a', 'b', 'c'),)
print(d[2][0:2]) # ('a', 'b')

print(c + d) # (1, 2, 3, 4, 100, 200, ('a', 'b', 'c'))
print(c * 3) # (1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)

# Tuple 함수

z = (5, 2, 3, 1, 5, 1)
print(z)
print(3 in z)
print(z.index(3))  # -> 찾고자 하는 값의 인덱스값 반환. (3은 2번째 인덱스에 있으므로 2 반환)
print(z.count(1))  # 값 개수 반환, 1 2개있으므로 2 반환.

Dictionary

=> 순서x, 중복x, 수정o, 삭제o

# 선언

a = {"name": "gyomni", "phone": "010-0101-0101", "birth": 980101}
b = {0: "Hello", 1: "coding"}
c = {"arr": [1, 2, 3, 4, 5]}

print(type(a)) # <class 'dict'>

# 출력

print(a["name"]) # gyomni
print(a.get("name")) # gyomni
print(a.get("address")) # None ( get은 없으면 none 출력해줌, 그냥 접근해도 되지만 get 사용하는 것을 더 선호. )
print(c["arr"][1:3]) # [2, 3]

# Dictionary 추가

a["address"] = "Seoul"
print(a) # {'name': 'gyomni', 'phone': '010-0101-0101', 'birth': 980101, 'address': 'Seoul'}

# List 추가

a["rank1"] = [1, 3, 4]  # {'name': 'gyomni', 'phone': '010-0101-0101', 'birth': 980101, 'address': 'Seoul', 'rank1': [1, 3, 4]}

# Tuple 추가

a["rank2"] = (
    1,
    2,
    3,
) 

print(a) # {'name': 'gyomni', 'phone': '010-0101-0101', 'birth': 980101, 'address': 'Seoul', 'rank1': [1, 3, 4], 'rank2': (1, 2, 3)}

# keys, values, items

print(a.keys()) # dict_keys(['name', 'phone', 'birth', 'address', 'rank1', 'rank2']) ( key만 list형태로 출력, but인덱스에는 접근 안됨.-> 형 변환 해야함. )
print(list(a.keys())) # ['name', 'phone', 'birth', 'address', 'rank1', 'rank2'] ( list로 형 변환. )

temp = list(a.keys())
print(temp[1:3]) # ['phone', 'birth'] ( 형 변환해서 인덱스로 접근 가능. )

print(list(a.items())) # [('name', 'gyomni'), ('phone', '010-0101-0101'), ('birth', 980101), ('address', 'Seoul'), ('rank1', [1, 3, 4]), ('rank2', (1, 2, 3))] ( list안에 튜플 들어간 형태로 반환. )
print(1 in b) # True ( key가 1인게 b에 있는지 )
print("name" in a) # True ( name이라는 key가 a에 있는지 ->True (in <-> not in) )
profile
Front-end developer 👩‍💻✍

0개의 댓글