본문 바로가기

딕셔너리8

반복문+데이터 구조 예제 1 #예제 : 변수에 입력된 값을 원화로 바꿔서 계산1 #방법1 prices = input('달러 단어를 붙여 금액을 입력해주세요\t:\t') exchange_rate = 1112 if prices[-2:] == '달러': print(f'{int(prices[:-2]) * exchange_rate} 원') elif prices[-3:] == ' 달러': print(f'{int(prices[:-3]) * exchange_rate} 원') #방법2 int_prices = int(prices[0:3]) #텍스트 인덱싱을 활용하여 숫자추출 및 정수로 형변환 total_price = int_prices * exchange_rate print(f'환율적용 금액출력\n:\t{total_price} 원') #예제 : 변수.. 2023. 12. 5.
데이터 자료구조 □ 튜플 ○ 데이터 구조 : 리스트, 튜플, 딕셔너리, 집합 ○ ( )를 이용해 선언할 수 있음 ○ 삭제나 추가가 불가능함 ○ 더하거나 반복하는 것은 가능함 #튜플 선언 tuple_data = (1,2,3,4,5,6,7,8,9,10) tuple_data_copy = (1,2,3,4,5,6,7,8,9,10) #튜플 조회 print(tuple_data) print(tuple_data_copy) print(tuple_data[0]) print(tuple_data[1]) print(tuple_data_copy[0]) print(tuple_data_copy[1]) #형번환 list_data = list(tuple_data) list_data_copy = list(tuple_data_copy) print(type(.. 2023. 12. 5.
enumerate() □ enumerate() 파이썬 내장함수로 열거형 객체를 반환하고 카운터 변수도 추가해줌 □ enumerate(iterable, start = 시작값) ○ iterable 길이만큼 인덱스를 생성해줌 ○ start 값을 정하여 시작 인덱스를 지정 가능 □ 아래 한 개의 리스트를 딕셔너리형식으로 변환하시오 l= ["red","black","blue","orange","purple"] #방법1 d1 = dict(enumerate(l)) #enumerate + 형변환 print(d1) d2 = dict(enumerate(l, start=100)) #형변환 print(d2) d3 = dict(enumerate(l)) print(d3) 2023. 11. 8.
딕셔너리 ↔ 자료구조 형변환 #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.
딕셔너리 값 추출 예제1 #아래 딕셔너리에서 출력결과와 같이 값 추출 #Name : Kim, Age : 23, Type : office' 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':'office', .. 2023. 11. 7.
[Python] 딕셔너리 □ 자료구조 딕셔너리 형태는 key와 value로 구성되어 있으며 { } 감쌈 □ 자주 사용하는 함수 : get(), values(), keys() □ json과는 다름. json은 javascript에서의 오브젝트이며 데이터를 교환하기 위한 규약된 폼임 #에제: 딕셔너리 합산 구하기 dict = {'a':17, 'b':114, 'c':247, 'd':362, 'e':220, 'f':728, 'g':-283, 'h':922} print(dict.values()) #딕셔너리 value 값 추출 : .values #방법① sum = 0 for i in dict: sum += dict[i] print(f'sum : {sum}') #방법② total = 0 for a in dict.values(): total .. 2023. 11. 1.
딕셔너리와 집합 ○ 딕셔너리(Dictionary) - 리스트나 튜플처럼 순차적으로 해당 요소의 값을 구하지 않고 키를 이용하여 값을 얻음 - 형식 : 딕셔너리명 = {'키1':'벨류','키2':'벨류','키3':'벨류'~~} - 벨류로 리스트를 넣을수 있음 dic = {'name':'ppp', 'phone':'000-0000-0000', 'birth':'0606', 'hobby':['soccer', 'golf']} #키만 조회 dic.keys() #벨류만 조회 dic.values() #키/벨류 추가 딕셔너리명[추가할 키명] = 벨류 dic['new'] = 'anything' #데이터삭제 딕셔너리명[키명] del dic['phone'] #for문을 통한 벨류값 추출 for i in dic.values(): print(i).. 2023. 10. 13.