ABOUT ME

Today
Yesterday
Total
  • '23.3.4(토) 파이썬 입문 <7: 딕셔너리>
    코딩/파이썬 2023. 3. 4. 17:57
    #파이썬 딕셔너리 
    # 범용적으로 가장 많이 사용
    # 딕셔너리 자료형(순서 x, 키 중복 x, 수정 0, 삭제 0)
    
    #선언
    a = {'name' : 'kim', 'phone' : '01022223333', } #key, value
    b = {
      'Name' : 'Niceman',
      'City' : 'Seoul',
      'Age' : 24,
    }
    e = dict(
      [
       ('Name', 'Niceman'),
        ('City','Seoul'),
        ('Age', 24)
      ]
    )
    
    f = dict(
      Name = 'Niceman',
      City = 'Seoul',
      Age = 24
    )
    print(a, type(a))
    print(b, type(b))
    print(e, type(e))
    print(f, type(f))
    
    #출력
    print('>>>>')
    
    print('a - ', a['name']) # key가 존재하지 않으면 에러 발생
    print('a - ', a.get('name1')) #key가 존재하지 않을경우 None 출력됨 즉, 져올때는 없을 경우를 위해 get을 쓴다! 
    
    print('>>>>')
    print('f - ', f.get('City'))
    
    #딕셔너리 추가
    print('>>>>')
    a ['address'] = 'Pohang' #키가 없어도 추가됨
    print('a - ', a)
    a['rank'] = [1,2,3]
    print('a - ', a)
    
    print(len(a)) #키의 개수를 셈 
    print(len(b))
    
    # dict_key, dict_values, dict_items : 반복문(__iter__)에서 사용가능
    print('>>>>')
    print('a - ' ,a.keys())
    print('b - ' ,b.keys())
    print('e - ' ,e.keys())
    print('f - ' ,f.keys())
    print('a.keys - ', list(a.keys())) #키를 리스트로 형변환
    print('a - ' ,a.values())
    print('b - ' ,b.values())
    print('e - ' ,e.values())
    print('a.vlaues - ', list(a.values())) #밸류를 리스트로 형변환
    
    print('>>>')
    print('a - ', a.items())
    print('a.items - ', list(a.items())) #리스트로 형변환
    
    
    print('>>>')
    print('a - ' , a.pop('name'))
    print('a - ' , a)
    
    print('>>>')
    print('f - ', f.popitem())
    print('f - ' , f)
    
    print('f - ', f.popitem())
    print('f - ' , f)
    
    print('f - ', f.popitem())
    print('f - ' , f)
    
    print('>>>>')
    print('a  - ', 'birth' in a) #a에 birth라는 키가 있어?
    print('a - ', 'phone' in a )
    
    # 수정
    print('>>>')
    a['test'] = 'test_dict'
    print('a -', a)
    
    a['address'] = 'Seoul'
    print('a -', a)
    
    print('>>>')
    a.update(birth = '35323') #수정 및 추가 가능한 method
    print('a -', a)
    
    temp = {'sex' : 'fm'}
    a.update(temp)
    print('a -', a)
    더보기

    {'Name': 'Niceman', 'City': 'Seoul', 'Age': 24} <class 'dict'>
    {'Name': 'Niceman', 'City': 'Seoul', 'Age': 24} <class 'dict'>
    {'Name': 'Niceman', 'City': 'Seoul', 'Age': 24} <class 'dict'>
    >>>>
    a -  kim
    a -  None
    {'name': 'kim', 'phone': '01022223333'} <class 'dict'>
    {'Name': 'Niceman', 'City': 'Seoul', 'Age': 24} <class 'dict'>
    {'Name': 'Niceman', 'City': 'Seoul', 'Age': 24} <class 'dict'>
    {'Name': 'Niceman', 'City': 'Seoul', 'Age': 24} <class 'dict'>
    >>>>
    a -  kim
    a -  None
    >>>>
    f -  Seoul
    >>>>
    a -  {'name': 'kim', 'phone': '01022223333', 'address': 'Pohang'}
    a -  {'name': 'kim', 'phone': '01022223333', 'address': 'Pohang', 'rank': [1, 2, 3]}
    4
    3
    >>>>
    a -  dict_keys(['name', 'phone', 'address', 'rank'])
    b -  dict_keys(['Name', 'City', 'Age'])
    e -  dict_keys(['Name', 'City', 'Age'])
    f -  dict_keys(['Name', 'City', 'Age'])
    a.keys -  ['name', 'phone', 'address', 'rank']
    a -  dict_values(['kim', '01022223333', 'Pohang', [1, 2, 3]])
    b -  dict_values(['Niceman', 'Seoul', 24])
    e -  dict_values(['Niceman', 'Seoul', 24])
    a.vlaues -  ['kim', '01022223333', 'Pohang', [1, 2, 3]]
    >>>
    a -  dict_items([('name', 'kim'), ('phone', '01022223333'), ('address', 'Pohang'), ('rank', [1, 2, 3])])
    a.items -  [('name', 'kim'), ('phone', '01022223333'), ('address', 'Pohang'), ('rank', [1, 2, 3])]
    >>>
    a -  kim
    a -  {'phone': '01022223333', 'address': 'Pohang', 'rank': [1, 2, 3]}
    >>>
    f -  ('Age', 24)
    f -  {'Name': 'Niceman', 'City': 'Seoul'}
    f -  ('City', 'Seoul')
    f -  {'Name': 'Niceman'}
    f -  ('Name', 'Niceman')
    f -  {}
    >>>>
    a  -  False
    a -  True
    >>>
    a - {'phone': '01022223333', 'address': 'Pohang', 'rank': [1, 2, 3], 'test': 'test_dict'}
    a - {'phone': '01022223333', 'address': 'Seoul', 'rank': [1, 2, 3], 'test': 'test_dict'}
    >>>
    a - {'phone': '01022223333', 'address': 'Seoul', 'rank': [1, 2, 3], 'test': 'test_dict', 'birth': '35323'}
    a - {'phone': '01022223333', 'address': 'Seoul', 'rank': [1, 2, 3], 'test': 'test_dict', 'birth': '35323', 'sex': 'fm'}

    댓글

Designed by Tistory.