Python_09_딕셔너리(dictionary)

hyeong taek jo·2023년 9월 11일

Python

목록 보기
9/53

📌딕셔너리(dictionary)

  • 자바로 치면 MAP가 비슷하다
  • key와 value를 갖는다.

예시

d = {'one':'hana', 'two':'dul', 'three': 'set'}
d['four'] = 'net' #새항목 삽입
print("d1=> ", d)

d['one'] = 1  #기존 항목의 값 변경
print('d2==>',d)
print('one' in d)  # 키에 대한 멤버쉽 테스트

d1=> {'one': 'hana', 'two': 'dul', 'three': 'set', 'four': 'net'}
d2==> {'one': 1, 'two': 'dul', 'three': 'set', 'four': 'net'}
True


d3 = {'one':1,'two':'둘',"three":'삼',"four":'사'}
print("d3.keys()==>",d3.keys())   # 키만 리스트로 추출함,임의의 순서
print("Type(d3.keys())==>",  type(d3.keys())) # 키만 리스트로 추출함, 임의의 순서
print("d3.values()=>" , type(d3.values())) # 값만 리스트로 추출함, 임의의 순서
print("d3.values()->",d3.values()) # 값만 리스트로 추출함, 임의의 순서
print("d3.items()->",d3.items()) # 키와 값의 튜플을 리스트로 반환

d3.keys()==> dict_keys(['one', 'two', 'three', 'four'])
Type(d3.keys())==> <class 'dict_keys'>
d3.values()=> <class 'dict_values'>
d3.values()-> dict_values([1, '둘', '삼', '사'])
d3.items()-> dict_items([('one', 1), ('two', '둘'), ('three', '삼'), ('four', '사')])

profile
마포구 주민

0개의 댓글