Python Learning Summary Four--this is the Python dictionary

Source: Internet
Author: User
Tags delete key shallow copy

The dictionary turns out to be the same thing.

Python learns now, and we already know that lists can come in handy if you want to group values into structures and reference them by number. Today, however, we will learn a data structure that references values by name, and we should know that this type of data is called mapping. The dictionary is the only built-in mapping type in Python, so let's learn the dictionary.

Understanding Dictionaries

Dictionaries are mutable and can store any number of Python objects. The following example is a simple dictionary type:

1 phonebook={'zhangsan':'12345','  Lisi':'54321'}2print Phonebook

A dictionary consists of multiple keys and their corresponding values (we also refer to key/value pairs as items). The dictionary includes a number of keys, one for each value. The key is unique in the dictionary, but the value is not unique. In the example above, the name is the key and the phone number is the value. Separated by a colon (:) between each key and its value, the items are separated by commas (,), and the entire dictionary is enclosed in curly braces. An empty dictionary (with no entries) consists of two curly braces, like this: {}.

dict function: we can call the Dict function to create a dictionary for other mappings (such as other dictionaries) or for sequences or keyword parameters (keys, values).

1 item=[('name','baiyishaonian'), ('  Age ','d= ')]2 dict (item)3 Print D


Basic operations

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

D[k] : Returns the value corresponding to the key k in dictionary D

d[k]=v : Associates The value V to the key K on dictionary D

del d[k] : Delete key k items

K in D : Check if key k exists in dictionary D

1b=='name':'Baiyishaonian',' Age':' at',' from':'Chongqing','Nowadd':'Jilin'}2 PrintLen (d)3 Printd['name']4d['name']='Huangsir'5 PrintD6 deld[' Age']7 PrintD8 Print ' from' inchD9 Print ' Age' inchD

Summary : Checking the membership of a key in a dictionary is more efficient than checking the values in the list, the larger the data size, the greater the difference in query efficiency.

dictionary format string : I have a format method for strings in a string, where the dictionary format string appears. Think about the similarities and differences, should you try to think about it? (Hint: the dictionary exists in a mapped way)

1d={'name':'Baiyishaonian',' Age':' at',' from':'Chongqing'}2 Print 'Introduce Yourself'3 Print 'My name is% (name) s, I\ ' m percent (age) s years old, I\ ' m from% (from) s.'% d

Dictionary methods

clear: Clears all items in the dictionary without a return value. (Use the following two examples to see where the clear method is characterized)

copy: Returns a new dictionary with the same key-value pair (shallow copy)

deepcopy : Deep Copy

1d={'Zhangsan':['789','123'],'Lisi':'12313'}2D2=d.copy ()3d['Zhangsan'].append ('999')4 PrintD5 PrintD26 7  fromCopyImportdeepcopy8d={'Zhangsan':['789','123'],'Lisi':'12313'}9d3=Deepcopy (d)Tend['Zhangsan'].append ('999') One PrintD A PrintD3

Fromkeys: Creates a new dictionary with the given key, and the default value for each key is none.

get: A more relaxed way to access the dictionary.

Generally speaking, if a key that does not exist in the dictionary is correctly accessed, an error occurs, then the GET method does not error, it returns none.

Has_key: Checks whether the dictionary contains the given key. The expression D.has_key (k) is equivalent to K in D.

Items and iteritems: Items return a dictionary item as a list, iteritems and items work the same, but it returns an iterator object instead of a list.

keys and Iterkeys: The Keys method returns the keys in the dictionary as a list, and the Iterkeys method returns the values in the dictionary as a list

pop: Gets the value corresponding to the given key and removes the key-value pair from the dictionary

values and itervalues: Corresponding to keys, Iterkeys, values method returns the value in the dictionary as a list (an iterator that itervalues the return value)

Summary : So far, we've learned the basics of string, tuple, list, and dictionary in Python, so how do we learn it? If and while statements? I think these looping statements can be naturally learned in the next Python example. So, next, I decided to continue learning Python's advanced programming and crawler knowledge.

Come on! Baiyishaonian.

Python Learning Summary Four--this is the Python 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.