Python dict addition, deletion, modification, query, and set () operations, pythondict

Source: Internet
Author: User

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

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.