ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Dictionary
    python/기본 2022. 4. 29. 14:59
    d = {'x': 10, 'y': 20}
    print(type(d))  # <class 'dict'>
    
    print(d['x'])  # 10
    d['x'] = 100
    print(d)  # {'x': 100, 'y': 20}
    d['z'] = 'ZZZ'
    print(d)  # {'x': 100, 'y': 20, 'z': 'ZZZ'}
    d[1] = 10000
    print(d)  # {'x': 100, 'y': 20, 'z': 'ZZZ', 1: 10000}
    
    print(dict(a=10, b=20))  # {'a': 10, 'b': 20}, 사전 생성의 또다른 방법
    
    ####################################################################
    d = {'x': 10, 'y': 20}
    print(help(dict))
    
    print(d.keys())  # dict_keys(['x', 'y'])
    print(d.values())  # dict_values([10, 20])
    
    d2 = {'x': 1000, 'j': 500}
    d.update(d2)
    print(d)  # {'x': 1000, 'y': 20, 'j': 500}
    
    d1 = {'a': 1}
    d2 = {'b': 2}
    d3 = d1 | d2
    print(d3)  # {'a': 1, 'b': 2}
    
    # d['z']  # KeyError: 'z'
    r = d.get('z')
    print(r)  # None
    print(type(r))  # <class 'NoneType'>
    rr = d.get('z', 0)
    
    print(d)  # 'x': 1000, 'y': 20, 'j': 500}
    d.pop('x')  # 1000
    print(d)  # {'y': 20, 'j': 500}
    del d['y']
    print(d)  # {'j': 500}
    
    d = {'x': 10, 'y': 20}
    d.clear()
    print(d)  # {}
    exist = 'a' in d
    print(exist)  # False
    
    ####################################################################
    x = {'a': 1}
    y = x
    y['a'] = 1000
    print(x)  # {'a': 1000}
    print(y)  # {'a': 1000}
    
    x = {'a': 1}
    y = x.copy()
    y['a'] = 1000
    print(x)  # {'a': 1}
    print(y)  # {'a': 1000}

    'python > 기본' 카테고리의 다른 글

    If  (0) 2022.04.29
    Set  (0) 2022.04.29
    Tuple  (0) 2022.04.29
    List  (0) 2022.04.29
    String  (0) 2022.04.24

    댓글

Designed by Tistory.