Python Learning: dictionaries (dictionary)

Source: Internet
Author: User

Dictionary is the most flexible of Python's built-in data structures. A list of lists is an ordered collection of objects, and dictionary is an unordered collection. The main difference is that the elements in the dictionary are accessed by keys rather than by offsets.

Variable length, heterogeneous, arbitrary nesting. Dictionaries can be modified in place, but are not supported for sequence operations in strings and lists. Because dictionaries are unordered collections, it is not feasible to operate in a fixed order.

1, the Construction of Dictionary:

d={} #空字典D = {' spam ': 2, ' Ham ': 1, ' Eggs ': 3} #三项目字典D = {' food ': {' spam ': 2, ' Ham ': 1, ' Eggs ': 3} #嵌套D = Dict.fromke YS ([' A ', ' B ']) #其他构造技术 {' A ': None, ' B ': none}d = dict (name= ' Bob ', age=42) #{' age ': ' Name ': ' Bob '}

In addition to the above several construction methods, there is a key and value corresponding technology:

>>>keyslist=[' spam ', ' ham ', ' eggs ']>>>valslist=[2,1,3]>>>d=dict (Zip (keyslist, valslist ) {' Eggs ': 3, ' Ham ': 1, ' spam ': 2}

Unlike list, dictionary is indexed with key:

>>>d[' eggs ']>>>d[' food ' [' Ham ']>>> ' eggs ' in D

The following three common operations return a list object:

>>>d.keys () #[' eggs ', ' ham ', ' spam ']>>>d.values () #[3, 1, 2]>>>d.items () #[(' Eggs ', 3), (' Ham ', 1), (' Spam ', 2)]

It is important to note that the above are all syntax in python2.7, Python3.0, which must be placed in the list call, the keys () in python3.0 returns an iterator:

>>>list (D.keys ()) >>>list (D.values ()) >>>list (D.items ())

The merge operation is as follows:

>>>d={' eggs ': 3, ' Ham ': 1, ' spam ': 2}>>>d2={' Turkey ': 6, ' Ham ': 2}>>>d.update (D2) #合并 >> ; >print (D) {' Turkey ': 6, ' eggs ': 3, ' Ham ': 2, ' spam ': 2}

As you can see from the above, the items of the same key during the merge merge, and if the key is the same but value is different, it will be updated.

There are two ways to delete an operation. Pop () deletes the value corresponding to this key. If you delete a nonexistent key, you will get an error:

>>>d.pop (' eggs ') 3>>>del d[' eggs ']


Other Dictionary methods:

Reading a nonexistent key tends to make an error, using the Get method to avoid errors and only returning none

>>>d.get (' spam ') #A key that's There2>>>print (D.get (' toast ')) #A key that's missingnone& Gt;>>d.get (' toast ') #if a key is missing, and is default88


The dictionary size comparison is no longer valid in Python3.0:

>>>d1 > D2 #Python2.6 Dictionary size comparison >>>sorted (D1.items ()) > Sorted (D2.items ()) #python3.0


Exercises:

1, two ways to create a list containing 5 0

>>>[0,0,0,0,0]>>>[0]*5

2, two ways to create a dictionary, there are two keys ' a ', ' B ', and each key associated with the value is 0.

>>>d={' A ': 0, ' B ': 0}>>>l1=[' A ', ' B ']>>>d = Dict.fromkeys (l1,0) >>>dict (a=0,b=0)

3. List four kinds of operations to modify the Dictionary object in situ

>>>d.pop (key) >>>d[' spam ']=10>>>d.update (D2) >>>del D (key)


Python Learning: dictionaries (dictionary)

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.