Getting Started with Python dictionaries

Source: Internet
Author: User

To view the Python version:

[Email protected] ~]# Python-vpython 2.7.5

1. Basic Dictionary operation

Case 1: Simple phone book implementation

[[email protected] ~]# Vim addresslist.py #-*-coding:utf-8-*-#一个简单的数据库通讯录 # Dictionary use person name as key. Each person uses another dictionary to indicate that its key ' phone ' and ' addr ' respectively represent the phone number and address people = {'        Wang ': {'                phone ': ' 12345 ',                ' addr ': ' BJ '        },         ' Ni ': {                ' phone ': ' 23456 ',                ' addr ': ' NJ '        },        ' Ma ': {                ' phone ': ' 34567 ', '                addr ': ' DJ '        }} #针对电话号码和地址使用的描述性标签, the output will be printed with labels = {        ' phone ': ' Phone number ',        ' addr ': ' address '} #查找电话号码还是地址name = raw _input (' Input name: ') request = Raw_input (' Phone number (p) or address (a)? ') #判断if request = = ' P ':        key = ' phone ' if request = = ' A ':        key = ' addr ' #只有名字在字典中才可以打印if name in people:        print " %s ' s%s is%s. "%  (Name,labels[key],people[name][key]) Else:        print ' sorry,i does not know '                  

Operation Result:

[email protected] ~]# python addresslist.py input name:wangphone number (p) or address (a)? Awang's address is BJ. [[email protected] ~]# python addresslist.py input name:ppppphone number (p) or address (a)? Psorry,i do not know

Access using the Get () method (compare to the difference in the previous example, omit the same code)

Key = Request     #请求既不是 ' a ', nor ' p ' if request = = ' P ':        key = ' phone ' if request = = ' A ':        key = ' addr ' #只有名字在字典中才可以打印 # if Name in people: #       print '%s '%s ' was%s. '%  (Name,labels[key],people[name][key]) #else: #       print ' sorry,i do not Know ' # #使用get () provides default value for person = People.get (name,{}) label = Labels.get (key,key) result = Person.get (key, ' not available ') print '%s '%s is%s. "% (Name,label,result)

  

Example output:

[email protected] ~]# python addresslist.py input name:wangphone number (p) or address (a)? Awang's address is BJ.
[[email protected] ~]# python addresslist.py   //Do not enter anything, because the default value is set, so the program does not error input Name:phone number (p) or address (a)? ' S is not  available.

  

2. formatting strings for dictionaries

(1). After the% in each conversion specifier, you can add the key (enclosed in parentheses), followed by the other explanatory elements.

>>> phonebook = {' Wang ': ' 1234 ', ' Ni ': ' 2345 ', ' Ma ': ' 3456 '}>>> "Wang ' s phone number is% (Wang) s." Phonebook "Wang ' s phone number is 1234."

(2). In addition to adding string keys, the conversion specifier can work as before. When using a dictionary in this way, any number of conversion specifiers can be used as long as all the given keys can be found in the dictionary. This type of string formatting is useful in the template system.

>>> template= "

The 3.update method can use one dictionary item to update another dictionary:

>>> D = {... ' title ': ' Python Web Site ',... ' URL ': ' http://www.python.org ',... ' Changed ': ' 2015-09-25 ' ...} >>> x={' title ': ' Python '}>>> d.update (x) >>> d{' url ': ' http://www.python.org ', ' changed ': ' 2015-09-25 ', ' title ': ' Python '}>>> x{' title ': ' Python '}

The items in the provided dictionary are added to the old dictionary and overwritten if they have the same key.

The 4.values method returns the values in the dictionary as a list

>>> d.values () [' http://www.python.org ', ' 2015-09-25 ', ' python ']

The 5.keys method will put the keys in the dictionary in the form of a list

The 6.items method returns all items in the dictionary as a list

>>> d.items (' url ', ' http://www.python.org '), (' Changed ', ' 2015-09-25 '), (' title ', ' Python ')]

Resources:

Basic Python Tutorial (Second edition. revision)

Getting Started with Python dictionaries

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.