Python Learning 3-index

Source: Internet
Author: User

Dictionary: All can be called by easy to find a specific word (key), so as to find his definition (value).

To create and use a dictionary:

>>> phonebook = {' Zhang ': ' 231 ', ' Wang ': ' 123 '}

  dict function : A dictionary can be established through the DICT function, through other mappings (such as other dictionaries) or by a sequence of (key, value) pairs

>>> Items  = [(' Name ', ' Zhang '), (' Age ', 2)]>>> d  = dict (items) >>> d{' Age ': 2, ' name ': ' Zhang '}>>> d[' name '] ' Zhang ' #dict function can also be created by keyword arguments to dictionary >>> d = dict (name = "Zhang ", age=2) >>> d{' age ': 2, ' name ': ' Zhang '}      

  Basic operation of the dictionary

The basic behavior of a dictionary is similar to a sequence in many ways

Len (d)--Returns the number of key-value pairs in D

D[K]--Returns the value of the key K

D[k] = v--assigns the V value to the K key (if K is not in D, it is automatically added to D, different from the sequence)

Del D[k]--delete the value of the key K

K in D--check if D contains an entry with key K (Note: The key is found)

  formatting strings for Dictionaries

Add a key after the% of each conversion specifier (in parentheses), followed by the other explanatory elements

>>> phonebook = {' Zhang ': ' 123 ', ' Wang ': ' 555 '}>>> "Zhang ' s phone number is% (Zhang) s."  Phonebook "Zhang ' s phone number is 123." 

The method of the dictionary

1.clear-Clears all items in the dictionary. No return value, or can be understood to return none

>>> d = {}>>> d[' name '] = ' Zhang ' >>> d[' age ' = 23>>> d{' age ': +, ' name ': ' ZH Ang '}>>> return_value = d.clear () >>> print return_value   

2.copy and Deepcopy replication

3.from_keys--Create a new dictionary using the key to the point

4.get more relaxed access to the dictionary method if the key does not exist returns none

5.has_key is the same as in method

6.items and Iteritems--keys and Iterkeys

Items--Returns the dictionary all items in a list, each item in the list is represented as a (key, value) pair, but not in any order

Iteritems-The function is approximately the same, but returns an iterator instead of a list

>>> d = {' Titile ': ' I am a tiger ', ' age ': Ten}>>> d.items (' age ', ten), (' Titile ', ' I am a tiger ' 
     
      )]>>> it =
       d.iteritems () >>> It<dictionary-itemiterator object at 0x02b136c0> 
     

7.pop--Used to get the value corresponding to the key and remove the pair of key values

>>> d{' age ': ten, ' titile ': ' I am a tiger '}>>> d.pop (' age ') 10>>> d{' titile ': ' I am a tiger '}
#不存在键, error >>> d.pop (' name ') Traceback (most recent): File "<pyshell#7>", line 1, in <module& Gt d.pop (' name ') keyerror: ' Name '

8.popitem Pop-up random entries and delete

>>> d{' age ': ten, ' titile ': ' I am a tiger '}>>> d.popitem (' age ', ten) >>> d{' titile ': ' I am a Tiger '}    

9.setdefault-Similar to the Get method, the ability to get the value associated with a given key, and to set the corresponding key value without a given key in the dictionary

>>> d = {}>>> d.setdefault (' name ', ' N/a ') ' N/a ' >>> d{' name ': ' N/a '}>> > d[' name '] = ' Zhang ' >>> d.setdefault (' name ', ' N/a ') ' Zhang ' >>> d{' name ': ' Zhang '} 

10.update--Update another dictionary with one dictionary, the updated dictionary will be added to the original dictionary, the same key will be overwritten

>>> d = {' title ': ' Zhang ', ' Age ': 10}
>>> x = {' Age ': 20}
>>> d.update (x)
>>> D
{' Age ': ' title ': ' Zhang '}
>>> y = {' sex ': ' Male '}
>>> d.update (y)
>>> D
{' Age ': ' Sex ': ' Male ', ' title ': ' Zhang '}

Python Learning 3-index

Related Article

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.