리스트 기본
□ 리스트(List) ○ 여러가지 관계가 있는 데이터를 하나의 변수로 다루는 방법 ○ 대괄호 [ ] 를 사용하여 데이터 저장 ○ ex = ['a','b','c','d','e',1,2,3,4,5,True,False] ○ 각 데이터는 인덱스를 사용해서 다룰 수 있음 □ 리스트 생성 location = ['서울시', '경기도', '인천시'] □ 인덱스 출력 location[0] #서울시 출력 location[1] #경기도 출력 location[2] #인천시 출력 □ 인덱스 추가 .append(추가 값) *복수개 추가는 불가 location.append('전라도') location.append('경상도') □ 인덱스를 활용한 슬라이싱 print(location[0:2]) print(location[3:5]) □..
2023. 11. 28.
딕셔너리 ↔ 자료구조 형변환
#json.dumps(dict,indent) #json.dump(dict, file_pointer) #json으로 형변환 d = {"group1":[ {'name':'Park', 'age':'32', 'sex':'Male'}, {'name':'Cho', 'age':'44', 'sex':'Female'}, {'name':'Kang', 'age':'39', 'sex':'Female', 'married':'No'} ], "group2":[ {'name':'Kim', 'age':'23', 'sex':'Male', 'married':'Yes'}, {'name':'Lee', 'age':'37', 'sex':'Male', 'married':'No'} ], "type": {'a':'employee', 'b':'offi..
2023. 11. 7.
딕셔너리 값 추출 예제2
#아래 딕셔너리에 group1에 {'name':'Park', 'age':'32', 'sex':'Male','married':'Yes'} 추가, type에 {'f':'engineer'} 추가 d = {"group1":[ {'name':'Park', 'age':'32', 'sex':'Male'}, {'name':'Cho', 'age':'44', 'sex':'Female'}, {'name':'Kang', 'age':'39', 'sex':'Female', 'married':'No'} ], "group2":[ {'name':'Kim', 'age':'23', 'sex':'Male', 'married':'Yes'}, {'name':'Lee', 'age':'37', 'sex':'Male', 'married':'No'}..
2023. 11. 7.
[Python] 중복제거(자료구조 set)
□ set : 집합자료형 중복 허용하지 않음 순서없음 □ List, Tuple : 순서있음, 중복허용 #아래 리스트에서 중복되는 원소를 제거 후 출력하세요. x = ["a", 1, "b", "a", 2, 3, "b", 4, 5, "b"] #방법1 ex1 = set(x) print(f'ex1 = {ex1}') #방법2(순서유지) from collections import OrderedDict #OrderedDict 라이브러리 호출 및 fromkeys 메서드 활용 ex2 = list(OrderedDict.fromkeys(x)) print(f'ex2 = {ex2}') #방법3(순서유지) ex3 = [] for i in x: if i not in ex3: ex3.append(i) print(f'ex3 = {ex..
2023. 11. 1.