Python data type, python

Source: Internet
Author: User

Python data type, python

A dictionary is a common and heavyweight data type in python consisting of a pair of key: value.

1. key, keys, values

A dictionary is a common and heavyweight data type in python consisting of a pair of key: value. key, keys, valuesid_db = {'jboss': {'mycis ': '10. 88.130.38 ', 'EC': '10. 88.130.29 ', 'rds': '10. 88.130.25 '}, 'jetty': {'mycis ': '10. 88.130.37 ', 'EC': '10. 88.130.36 '} print (id_db) for key in id_db: # dictionary key print (key, id_db [key]) key_list = id_db.keys () # dictionary keys attribute value_list = id_db.values () # dictionary values attribute print (key_list) print (value_list) # running result: {'jbos': {'rds': '10. 88.130.25 ', 'EC': '10. 88.130.29 ', 'mycis': '10. 88.130.38 '}, 'jetty': {'EC': '10. 88.130.36 ', 'mycis': '10. 88.130.37 '} jboss {'rds': '10. 88.130.25 ', 'EC': '10. 88.130.29 ', 'mycis': '10. 88.130.38 '} jetty {'EC': '10. 88.130.36 ', 'mycis': '10. 88.130.37 '} dict_keys (['jboss', 'jetty']) dict_values ([{'rds': '10. 88.130.25 ', 'EC': '10. 88.130.29 ', 'mycis': '10. 88.130.38 '}, {'EC': '10. 88.130.36 ', 'mycis': '10. 88.130.37 '}])

2. dictionary query dir [index], dir. get [index]

Server_list = {'uat': {'jbos': 'fca-vm-uat-jbos', 'jetty ': 'fca-vm-uat-jetty '}, 'dev': {'jbos': 'fca-vm-dev-jbos', 'jetty ': 'fca-vm-dev-jetty'}, 'prod ': {'jbos': 'fca-vm-prod-jbos', 'jetty ': 'fca-vm-prod-jetty'} print (server_list) print ("first query method") print (server_list ['uat']) # if this does not exist, the program reports an error print ("second query method ") print (server_list.get ('test') # if such a key does not exist, None is returned. get () # Test Result: {'prod': {'jetty ': 'fca-vm-prod-jetty', 'jbos ': 'fca-vm-prod-jbos'}, 'dev': {'jetty ': 'fca-vm-dev-jetty', 'jbos ': 'fca-vm-dev-jbos'}, 'uat': {'jetty ': 'fca-vm-uat-jetty', 'jbos ': 'fca-vm-uat-jbos'} first query method {'jetty ': 'fca-vm-uat-jetty', 'jbos ': 'fca-vm-uat-jbos'} second query method None

3. Add/change elements to the dictionary. Directly index and assign values. If this key is available, change it. If this key is not available, increase it.

Server_list = {'uat': {'jbos': 'fca-vm-uat-jbos', 'jetty ': 'fca-vm-uat-jetty '}, 'dev': {'jbos': 'fca-vm-dev-jbos', 'jetty ': 'fca-vm-dev-jetty'}, 'prod ': {'jbos': 'fca-vm-prod-jbos', 'jetty ': 'fca-vm-prod-jetty'} print (server_list) # Add the dictionary element server_list ['test'] = {'jbos': 'fca-vm-test ',} server_list ['dev'] ['apache '] = 'dev-apache' # change the dictionary valueserver_list ['uat'] ['jbos'] = '10. 88.130.38 '{'dev': {'jbos': 'fca-vm-dev-jbos', 'jetty': 'fca-vm-dev-jetty '}, 'uat': {'jbos': 'fca-vm-uat-jbos', 'jetty ': 'fca-vm-uat-jetty'}, 'prod ': {'jbos': 'fca-vm-prod-jbos', 'jetty ': 'fca-vm-prod-jetty'} {'test': {'jbos ': 'fca-vm-test'}, 'dev': {'jbos': 'fca-vm-dev-jbos', 'jetty ': 'fca-vm-dev-jetty ', 'apache': 'dev-apache'}, 'uat': {'jbos': '10. 88.130.38 ', 'jetty': 'fca-vm-uat-jetty '}, 'prod': {'jbos': 'fca-vm-prod-jbos ', 'jetty ': 'fca-vm-prod-jetty'} # running result:

4. Delete dictionary elements: del, pop

Server_list = {'uat': {'jbos': 'fca-vm-uat-jbos', 'jetty ': 'fca-vm-uat-jetty '}, 'dev': {'jbos': 'fca-vm-dev-jbos', 'jetty ': 'fca-vm-dev-jetty'}, 'prod ': {'jbos': 'fca-vm-prod-jbos', 'jetty ': 'fca-vm-prod-jetty'} print (server_list) del server_list ['uat'] server_list ['dev']. pop ('jbos') print (server_list) # running result: {'prod': {'jetty ': 'fca-vm-prod-jetty', 'jbos ': 'fca-vm-prod-jbos'}, 'dev': {'jetty ': 'fca-vm-dev-jetty', 'jbos ': 'fca-vm-dev-jbos'}, 'uat': {'jetty ': 'fca-vm-uat-jetty', 'jbos ': 'fca-vm-uat-jbos' }}{ 'prod': {'jetty ': 'fca-vm-prod-jetty', 'jbos ': 'fca-vm-prod-jbos'}, 'dev': {'jetty ': 'fca-vm-dev-jetty '}}

5. update attributes of the dictionary

Dir1 = {'id': 6103241990, 'name': 'xiaoping', 'age': 26} dir2 = {'id': 6103241988, 'name': 'ruiping ', 'Sex ': 'Woman'} print (dir1) print (dir2) dir1.update (dir2) # If dir2 has a key in dir1, updatedir1 is retained. If no key exists, dir1print (dir1) is retained) print (dir2) # Run the result {'id': 6103241990, 'age': 26, 'name': 'xiaoping'} {'id': 6103241988, 'sex ': 'Woman ', 'name': 'ruiping'} {'id': 6103241988, 'age': 26, 'sex': 'Woman', 'name ': 'ruiping'} {'id': 6103241988, 'sex': 'Woman ', 'name': 'ruiping '}
Update

6. items converts dictionaries to lists. It is not recommended to use

>>> a  = {'name':'bianxiaoping', 'sex':'man'}>>> a.items()dict_items([('name', 'bianxiaoping'), ('sex', 'man')])>>>

7. setdefault (if key exit, return a [key], if not a [key] = 'not exist'

""" D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """
>>> a  = {'name':'bianxiaoping', 'sex':'man'}>>> a.setdefault('age')>>> a{'age': None, 'name': 'bianxiaoping', 'sex': 'man'}>>> a.setdefault('name')'bianxiaoping'>>> a.setdefault('job','Not exist')'Not exist'>>> a{'age': None, 'name': 'bianxiaoping', 'sex': 'man', 'job': 'Not exist'}

 

8. The contained relationship in the dictionary (only for the key)

>>> a  = {'name':'bianxiaoping', 'sex':'man'}>>> 'sex' in aTrue>>> 'age' in aFalse>>> 'man' in aFalse>>>

 

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.