1) list [,]
1. 생성: list=[ ... ]
2. 추가:
1) 뒤에 이어붙이기 : list.append("element_name")
2) 특정 index에 삽입 : list.insert(1, "element_name")
3. 삭제:
1) index로 삭제 : del list[0]
2) 특정 element로 삭제 : .remove( )
3) 마지막 element/ 특정index 삭제하고 반환 : .pop()/ .pop(3)
4. 호출: list[0]
5. 리스트 합치기: int, str 상관없이 '+'
6. 정렬: .sort()
- 역순 정렬: .sort(reverse=True)
7. 리스트의 특징
: 번거로움을 최소화하기 위해 덮어쓰기 안함 (e.g., a = a.sort() # >에러)
- list.append(), list.remove(), list.sort()
2) dict {:}
1. 생성: dict = {key1: value1, key2: value2, ...}
2. 추가: dict["key"]="value"
3. 삭제:
1) del dict["key"]
2) 전체 삭제: dict.clear()
4. 호출:
- key 값으로:
1) dict["key"] # >value값 출력
2) dict.get("key", "key값이 없을 경우 출력될 default값") : 없는 key값을 넣어도 key error가 안난다는 장점
- value 값으로 : dict.values()
- item 값으로 : dict.items() # key+value; 튜플 형태로 출력됨
- 유무 확인:
1) dict["key"] in dict / dict["key"] in dict.keys()
2) dict["values"] in dict.values()
5. 반복문:
for x, y in dict.items():
print(x, y)
6. 적용
1) 적용사례1 (dict)
# {id1: password1,
# id2: password2,
# ...
# id~: password~}
total_dictionary = {}
while True:
id = input("아이디를 입력하세요: ")
if id == 'q':
break
else:
total_dictionary[id] = ""
for i in total_dictionary:
print(i)
password = input("비밀번호를 입력해주세요 : ")
total_dictionary[i] = password
print(total_dictionary)
2) 적용사례2 (list + dict)
# [{'아이디': 'id1', '비밀번호': 'password1'},
# {'아이디': 'id2', '비밀번호': 'password2'},
# ...
# {'아이디': 'id~', '비밀번호': 'password~'}]
total_list = []
while True:
id = input("아이디를 입력하세요: ")
if id == 'q':
break
else:
total.list.append({"아이디": id, "비밀번호": ""})
for i in total_list:
print(i['아이디'])
password = input("비밀번호를 입력하세요: ")
i["비밀번호"] = password
print(total_list)
3) set {,}
- 집합
- list와 달리, (1) 순서가 상관이 없고, (2) 겹치는 요소가 존재하지 않는다.
- list에서 중복을 제거하고, 다시 list로 만들 때 유용하게 사용
# 중복 제거
list(set(temp))
# 중복 제거 후 개수 확인
len(set(temp))
- | (합집합), & (교집합), - (차집합)은 집합과 집합끼리만 할 수 있는 연산이다.
- 집합 연산을 하고 싶은 경우:
# 1. 문자열
# "문자열" > ["문자열"] > set(["문자열"])
# string > list > set
set(["문자열"]) - set(["문자열"])
# 2. 리스트
# 교집합, 차집합, 합집합 찾을 때 list에서 set으로 전환하고 찾을 수 있음
s1 = [1,2,3,10,10,10,20,20,20]
s2 = [3,3,3,4,10,20,40]
set(s1).intersection(set(s2))
- list로 변환하고 싶은 경우: list(set)
4) tuple (,)
- 값과 크기가 변하지 않으며, 수정이 불가능하다
- 호출: index로
- 함수에서 2개 이상을 return했을 때 이를 하나의 변수로 받으면 tuple로 출력된다
+ 추가로 알아두면 좋은 것 : index로 호출? key값으로 호출?
temp[???] # temp라는 그룹형 변수에서 ???에 해당하는 멤버를 꺼낸다.
??? <- index number : str, list, tuple, numpy.array() <- (배열/벡터/)수치행렬
??? <- key : dict, pandas.DataFrame() <- 엑셀
'멋쟁이 사자처럼 AI SCHOOL 5기 > Today I Learned' 카테고리의 다른 글
| [2주차 총정리] pd.DataFrame 기초 함수, hist 함수, pivot_table, 결측값 처리 방법 (0) | 2022.03.22 |
|---|---|
| [2주차 총정리] Python 초보자를 위한 class 기초 총정리 (0) | 2022.03.22 |
| [2주차 총정리] open 함수 이용하여 텍스트 파일(.txt, .csv, ...) 읽고 쓰기 (+ pickle과의 차이점) (0) | 2022.03.22 |
| [2주차 총정리] Python 반복문 유용한 기능(enumerate, zip, tqdm) 및 함수(Counter) 총정리 (0) | 2022.03.21 |
| [1주차 총정리] Jupyter Notebook 편리한 키보드 단축키(Shortcuts) 총정리 (0) | 2022.03.21 |