Python's first basic Tutorial 4 Chapter Dictionary: When the index does not work also

Source: Internet
Author: User

Python 2.7.5 (default, May, 22:43:36) [MSC v.1500 + bit (Intel)] on Win32type "copyright", "credits" or "license ( "For more information. #4.1 Dictionary use >>> names=[' Alice ', ' Beth ', ' Cecil ', ' Dee-dee ', ' Earl ']>>> numbers=[ ' 2341 ', ' 9102 ', ' 3158 ', ' 042 ', ' 5551 ']>>> names=[' Alice ', ' Beth ', ' Cecil ', ' Dee-dee ', ' Earl ']>>> numbers=[' 2341 ', ' 9102 ', ' 3158 ', ' 042 ', ' 5551 ']>>> numbers[names.index (' Cecil ')] ' 3158 ' >>> 014298 >>> 0912syntaxerror:invalid token>>> 0812syntaxerror:invalid token#4.2 Creating and using dictionaries >>> phonebook={' Alice ': ' 2341 ', ' Beth ': ' 9102 ', ' Cecil ': ' 3258 '}>>> phonebook[' Cecil '] ' 3258 ' #4.2.1 Dict functions > >> items=[(' name ', ' Gumby '), (' Age ', "]>>>") d = dict (items) >>> d{' age ': "The ' name ': ' Gumby '}> >> d[' name '] ' Gumby ' >>> d = dict (name= ' Gumby ', age=42) >>> d{' age ':, ' name ': ' Gumby '} #4.2.2 The Basic dictionary Operation #len (d) Returns the number of items in D (key-value pairs) #d[k] Returns the value associated to the key K #d[k]=v the value V is associated to the key K #del D[k] Delete the item with the key K #k in D checks if D includes the key KThe type of the item # Key: The dictionary key can be integer, float (real), string, or tuple. The key of a dictionary can be any immutable type. #在字典中检查键的成员资格比在列表中检查值的成员资格更有效, the larger the size of the data structure, the more obvious the efficiency gap between them >>> x=[]>>> x[42]= ' Foobar ' Traceback (most recent): File "<pyshell#24>", line 1, <module> x[42]= ' Foobar ' Indexerro R:list Assignment index out of range>>> x={}>>> x[42]= ' Foobar ' >>> x{42: ' Foobar '} #代码清单4-1 dictionary Sample #4.2.3 Dictionary formatted string >>> phonebook{' Beth ': ' 9102 ', ' Alice ': ' 2341 ', ' Cecil ': ' 3258 '}>>> ' Cecil ' phone Number is% (Cecil) s. "% phonebook" Cecil's phone number is 3258. ">>> template =" ' 

Sample Dictionary Demo

#coding =utf-8#e4-1.py Dictionary Demo sample # Simple Database # Use the name as a dictionary of keys, each with a dictionary, with the key ' phone ' and ' addr ' representing their phone number and address respectively. People = {' Alice ': {' Phone ': ' 2341 ', ' addr ': ' Foo Drive ', ' Beth ': {' phone ': ' 9102 ', ' addr ': ' Bar  Street '}, ' Cecil ': {' phone ': ' 3158 ', ' Addr ': ' Baz Avenue '}} #针对电话号码和地址使用的描写叙述性标签, will be used when printing output labels = {' Phone ': ' Phone number ', ' addr ': ' Address '}name=raw_ Input (' Name: ') #查找电话号码还是地址?

Use the correct key: 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]) #python e4-1.py#name:beth#phone number (p) or address (a)? P#beth ' s phone number is 9102.


Sample Dictionary Method demo

#coding =utf-8#e4-2# A simple database with Get () # Add code to insert the database in code listing 4-1 people = {' Alice ': {' phone ': ' 2341 ', ' addr ': ' Foo drive 23 '}, ' Beth ': {' phone ': ' 9102 ', ' addr ': ' Bar  Street '}, ' Cecil ': {' phone ': ' 3158 ', ' addr ': ' Baz avenue '}}labels = {' Phone ' : ' Phone number ', ' addr ': ' address '}name = raw_input (' name: ') #查找电话号码还是地址? request = Raw_input (' Phone number (p) or address ( a)? ') #使用正确的键key = Request #假设请求既不是 ' P ' is also not ' a ' if request = = ' P ': key = ' phone ' if request = = ' A ': key = ' addr ' #使用get () provides default value: Per son = People.get (name, {}) label = Labels.get (key, key) result = Person.get (key, ' not available '), print '%s '%s ' is%s. "% ( Name, label, result) #python e4-2.py#name:beth#phone number (p) or address (a)? A#beth's address is Bar  Street. #python e4-2.py#name:cecil#phone Number (p) or address (a)? P#cecil ' s Phone number I S 3158. #python e4-2.py#name:cecil#phone Number (p) or address (a)?

X#cecil ' s x is not available.



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Python's first basic Tutorial 4 Chapter Dictionary: When the index does not work also

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.