python basics dictionary dict

Dictionary -dict

  • Purpose:

  • Definition method: store data through {} and key:value (key value pair). Each key value pair is separated by comma. In the key value pair, the key is an immutable data type, and the value is any data type

    def dict_setd():
        # The first way of definition
        d1 = {'name': 'egon', 'age': 84}
        # The second way of definition
        d2 = dict({'name': 'egon', 'age': 84})
        # The third way of definition
        l1 = ['name', 'age']
        l2 = ['egon', 18]
        z1 = zip(l1, l2)
        d3 = dict(z1)
        # Print defined dictionary
        print(d1)
        print(d2)
        print(d3)
    dict_setd()

Common methods:

  • Take the value according to the key: value mapping relationship, which can be saved or retrieved

    # It can be stored or retrieved according to the key:value mapping relationship.
    def dict_getvalue():
        d1 = {'name': 'egon', 'age': 73}
        print(d1)  # Take out the value in d1
        # When the key exists in the dictionary, modify the value value. If the key does not exist, add a new key value pair
        d1['name'] = 'alex'
        d1['gender'] = 'male'
        print(d1)
    dict_getvalue()
  • Member judgment in / not in, return Boolean value

    # Member operation in / not in: determines whether the key exists in the dictionary (not the value), and returns the Boolean value
    def dict_in():
        d1 = {'name': 'egon', 'age': 73}
        print('egon' in d1)
    dict_in()
  • len: get the number of key value pairs in the current dictionary

    # len: get the number of key value pairs in the current dictionary
    def dict_len():
        d = {'name': 'alex', 'age': 73, 'gender': 'male'}
        print(len(d))
    dict_len()
  • get: gets the value of the specified key. If the value does not exist, it returns None by default. You can modify the default return value

    # get: gets the value of the specified key. If the value does not exist, it returns None by default. You can modify the default return value
    def dict_get():
        d = {'name': 'alex', 'age': 73}
        print(d.get('gender'))  # Result None
        print(d.get('gender', 'male'))  # Result male
    dict_get()
  • keys,values,items: get the key, value, and key value pairs in the dictionary

    # keys,values,items
    def dict_kvi():
        d = {'name': 'alex', 'age': 73}
        print(d.keys())  # The result is: return the list of all keys: ['name','age ']
        print(d.values())  # The result is: return a list of all values: ['alex',73]
        print(d.items())  # The result is: all key value pairs are returned, in which the return value is in the form of a list set of tuples, and each key value pair exists in the tuple
    
        # According to key value pair or key and value
        for key, value in d.items():
            print(key, value)
    dict_kvi()
  • Pop: pop up the corresponding key value pair through the specified key, and the return value is the value in the key value pair

    # pop: delete. pop up the specified key. The return value is the value corresponding to the key
    def dict_pop():
        d = {'name': 'alex', 'age': 73, 'gender': 'male'}
        a = d.pop('name')
        print(d)  # There is no more 'name' in the dictionary: the key value pair 'Alex'
        print(a)  # Value corresponding to 'name': alex
    dict_pop()
  • popitem: pop up a set of key value pairs randomly. The return value is the key value pair information in the form of tuples

    # popitem: pop up a set of key value pairs randomly, and the return value is the key value pairs shown in tuples
    def dict_popitem():
        d = {'name': 'alex', 'age': 73, 'gender': 'male'}
        a = d.popitem()
        print(a)
        print(d)
    dict_popitem()
  • update: replace old dictionary with new one

    # update: replace the dictionary with a new one
    def dict_upd():
        d = {'name': 'alex', 'age': 73, 'gender': 'male'}
        d.update({'hobby':'read'})  # Add the key value pair to the dictionary when it does not exist
        d.update({'name':'egon'})   # When the key value pair to be replaced exists, directly modify the value of the key value pair
        print(d)
    dict_upd()
  • fromkeys: two parameters are required to generate a new dictionary. The first parameter must be an iteratable object. Here, the elements in the iteratable object will be used as the key in the dictionary in turn. The second parameter is the value value of the key to form a new dictionary

    # dict.fromkey: a new dictionary is generated. The first parameter is the iteratable object, which takes the elements in the iteratable object as the key,
    # The second parameter, as the value of the element in the iteratable object, forms a new dictionary
    def dict_fromkey():
        print(dict.fromkeys([1,2,3],['ke','k1']))
    dict_fromkey()
  • setdefault: when the specified key exists, the content of the key value pair is not modified, and the value value of the key value pair is returned; if not, the specified key and value are added to the dictionary as new key value pairs, and the value is returned

    # setdefault: when the specified key does not exist in the dictionary, add the key value pair and return value; if it exists, do not modify the key value pair and return value
    def dict_sd():
        d = {'name': 'alex', 'age': 73, 'gender': 'male'}
        print(d.setdefault("hobby","read"))
        print(d)
        print(d.setdefault("name","egon"))
        print(d)
    dict_sd()
  • Dictionary dict is an unordered, variable data type that can store multiple values

Tags: Python

Posted on Wed, 06 Nov 2019 01:47:14 -0800 by Dawg