Python dict addition, deletion, modification, query, and set () operations, pythondict
# Add, delete, modify, query, and update dict: my_dict = {'name': 'Dragon', 'age': 18, 'sex ': 'male'} # Add my_dict ['height'] = '000000' # Delete my_dict.pop ('sex ') # Change my_dict ['age'] = '19' # query print ('name' in my_dict) # get () the method features the same print (my_dict.get ('name', 'object') as in) # The second parameter is the parameter returned by the key value that cannot be found # Dictionary Update (merge dictionary) curry_dict = {'job': 'nba ', 'height': '000000'} my_dict.update (Curry_dict) # {'name': 'Dragon', 'age ': '19', 'height': '200', 'job': 'nba '} # set () set operation # a_list = [193cm, 2, 3] # print (list (set (a_list) # [1, 2, 3, 4, 5] my_set = {1, 2, 3, 4, 5} # Add my_set.add (10) # Note that if the element already exists, it will not be added # Delete my_set.remove (1) # del my_set # Delete the set itself # query print (4 in my_set) # update my_set.update ({1, 2, 3}) print (my_set) # set () operation a_set = {1, 2, 3, 4, 5} B _set = {4, 5, 6, 7, 8} # print (a_set & B _set) # output duplicate elements of two sets # print (a_set | B _set) # {1, 2, 3, 4, 5, 6, 7, 8 }#-difference set print (a_set-B _set) # subtract the remaining elements from the public part output # variance or print (a_set ^ B _set) # All elements except the output repeat # also mention the unchangeable set test_set = frozenset ({1, 2, 3, 4, 5, 2, 1}) # test_set.add (2) # Error # test_set.remove (3) # print (test_set) # deduplication