1. 파이썬 자료형 - [리스트]

<aside> 💡 - 지난번에 배웠던 문자열 스트링도 리스트다!!

</aside>

#1. 리스트란?

— 리스트 생성 —

integers = [-3, -2, -1, 0, 1, 2, 3]
print(integers)  #[-3, -2, -1, 0, 1, 2, 3]

staff_list = ["David", "Maria", "John", "Johanson", "RoDaJu"]
"""
staff_list = [
		"David", 
		"Maria", 
		"John", 
		"Johanson", 
		"RoDaJu"
]
"""
print(staff_list)  #["David", "Maria", "John", "Johanson", "RoDaJu"]

empty_list = []
print(empty_list)  #[]

# 리스트를 품은 리스트! (2차원)
twoD_map = [
    [0, 1, 2, 3, 4],
    [5, 6, 7, 8, 9],   
    [10, 11, 12, 13, 14],
	  [15, 16, 17, 18, 19],
    [20, 21, 22, 23, 24],
]

# 다 들어감
WTF_list = [["David", "Maria", "John", "Johanson", "RoDaJu"], 12, (1, 2), "everything"]

— element 접근 —

"""
1. 인덱싱
"""
integers[3]         # 0
integers[-1]        # 3
integers[0]         # -3

staff_list[0]       # David

twoD_map[0]         # [0, 1, 2, 3, 4]
twoD_map[-1]        # [20, 21, 22, 23, 24]
twoD_map[0][0]      # 0
twoD_map[3][-1]     # 19

WTF_list[2]         # (1, 2)
WTF_list[2][1]      # 2
WTF_list[0]         # ["David", "Maria", "John", "Johanson", "RoDaJu"]

"""
2. 슬라이싱
"""
# integers = [-3, -2, -1, 0, 1, 2, 3]
integers[1:4]       # [-2, -1 , 0]
integers[:4]        # [-3, -2, -1, 0]
integers[1:]        # [-2, -1, 0, 1, 2, 3]
integers[::-1]      # [3, 2, 1, 0, -1, -2, -3]

twoD_map[2:4]       # [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]
twoD_map[::-1]      # [[20, 21, 22, 23, 24], [15, 16, 17, 18, 19], [10, 11, 12, 13, 14], [5, 6, 7, 8, 9], [0, 1, 2, 3, 4]]
twoD_map[0][:3]     # [0, 1, 2]
twoD_map[0][::-1]   # [4, 3, 2, 1, 0]

"""
3. 인덱스 초과하면 IndexError: list index out of range
"""
print(integers[10])  # IndexError: list index out of range

#2. 리스트 관련 연산

— 탐색 —

# staff_list = ["David", "Maria", "John", "Johanson", "RoDaJu"]

"""
#. in 을 사용
"""
director = "David"
if director in staff_list:
	print(director, "is one of the staffs!")
	print(staff_list.index(director), "번째 스태프!")

stranger = "me"
print(staff_list.index(stranger))  # ValueError: 'me' is not in list

sentence = "cats, dogs and birds are all animal"
if "cat" in sentence:
	print("yes!!")

— 추가 —

# staff_list = ["David", "Maria", "John", "Johanson", "RoDaJu"]
# stranger = "me" 를 추가해보자!

"""
#1. insert(idx, value)
"""
staff_list.insert(1, stranger) # ['David', 'Maria', 'John', 'Johanson', 'RoDaJu']
staff_list.insert(6, stranger)  # ['David', 'me', 'Maria', 'John', 'Johanson', 'RoDaJu', 'me']
staff_list.insert(10, stranger) # ['David', 'me', 'Maria', 'John', 'Johanson', 'RoDaJu', 'me', 'me']

"""
#2. append(value)
"""
staff_list.append(stranger)  # ['David', 'Maria', 'John', 'Johanson', 'RoDaJu', 'me']
staff_list.append(stranger)  # ['David', 'Maria', 'John', 'Johanson', 'RoDaJu', 'me', 'me']

"""
#3. extend
"""
staff_list = ["David", "Maria", "John", "Johanson", "RoDaJu"]
new_staff = ['Abraham', 'Sarah', 'Thomas', 'friends']
staff_list.extend(new_staff)     # ['David', 'Maria', 'John', 'Johanson', 'RoDaJu', 'Abraham', 'Sarah', 'Thomas', 'friends']
staff_list.extend(['you', 'me']) # ['David', 'Maria', 'John', 'Johanson', 'RoDaJu', 'Abraham', 'Sarah', 'Thomas', 'friends', 'you', 'me']

"""
#4. 더하기
"""
['a'] + ['b']  # ['a', 'b']

intern_list = ['a', 'b', 'c', 'd', 'e']
staff_list = ["David", "Maria", "John", "Johanson", "RoDaJu"]
total_list = staff_list + intern_list 

staff_list = staff_list + intern_list
staff_list += intern_list

— 수정 —

# integers = [-3, -2, -1, 0, 1, 2, 3]
integers[1] = 0            # [-3, 0, -1, 0, 1, 2, 3]     
integers[1] = 'hey'        # [-3, hey, -1, 0, 1, 2, 3]

# twoD_map = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]
twoD_map[2] = -12          # [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], -12, [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]
twoD_map[2][3] = 'cute'    # [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 'cute', 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]

— 제거 —

staff_list = ["David", "Maria", "John", "Johanson", "RoDaJu"]

"""
#1. del
"""
del staff_list[6]
del staff_list [5:]

"""
#2. remove
"""
staff_list.remove("David")
staff_list.remove("Newton")  # Value error

# remove와 같은 의미
del staff_list(staff_list.index("David"))
del staff_list(staff_list.index("Newton"))  # Value error