Python learning Summary 4. python Summary
This is the case with the dictionary.
Python has learned that if you want to group values into structures and reference them by number, the list can be used. However, today we will learn a data structure that references values by name. We should know that this data type is called ing. The dictionary is the only built-in ing type in Python, so we will learn the dictionary.
Cognition dictionary
The dictionary is variable and can store any number of Python objects. The following example shows a simple dictionary type:
1 phonebook={'zhangsan':'12345','lisi':'54321'}2 print phonebook
A dictionary consists of multiple keys and their corresponding values (we also call a key/value pair an item ). The dictionary contains many keys, each of which corresponds to a value. The key is unique in the dictionary, but the value is not unique. In the preceding example, the name is a key and the phone number is a value. Each key and its value are separated by a colon (:), and items are separated by commas (,), and the entire dictionary is enclosed in braces. An empty dictionary (excluding any item) consists of two braces, such :{}.
Dict function: You can call the dict function to create a dictionary based on sequences or keyword parameters such as other mappings (such as other dictionaries) or values.
1 item=[('name','BaiYiShaoNian'),('age','23')]2 d=dict(item)3 print d
Basic operations
Len (d): Returns the number of keys (key-value pairs) in dictionary d.
D [k]: Returns the value of key in the dictionary d.
D [k] = v: Associate value v with the key k of dictionary d.
Del d [k]: Delete key k items
K in d: Check whether the key k exists in dictionary d.
1 d={'name':'BaiYiShaoNian','age':'23','from':'ChongQing','nowAdd':'Jilin'}2 print len(d)3 print d['name']4 d['name']='huangSir'5 print d6 del d['age']7 print d8 print 'from' in d 9 print 'age' in d
Summary: checking the key membership in the dictionary is more efficient than checking the value in the list. The larger the data size, the greater the query efficiency difference between the two.
Dictionary formatting string: I have discussed the string formatting method in the string article. Here, the dictionary formatting string appears again. Think about the similarities and differences. Should you try to think about them? (Note: The dictionary exists in ing mode)
1 d={'name':'BaiYiShaoNian','age':'23','from':'ChongQing'}2 print 'introduce yourself'3 print 'My name is %(name)s , I\'m %(age)s years old , I\'m from %(from)s .' % d
Dictionary Method
Clear: Clear all items in the dictionary and no return value. (The following two examples show the features of the clear method)
Copy: Returns a new dictionary with the same key-value pairs (Shortest copy)
Deepcopy: Deep Replication
1 d={'zhangsan':['789','123'],'lisi':'12313'} 2 d2=d.copy() 3 d['zhangsan'].append('999') 4 print d 5 print d2 6 7 from copy import deepcopy 8 d={'zhangsan':['789','123'],'lisi':'12313'} 9 d3=deepcopy(d)10 d['zhangsan'].append('999')11 print d12 print d3
Fromkeys: Create a new dictionary with the given key. The default value of each key is None.
Get: A more loose way to access the dictionary.
In general, if a key that does not exist in the dictionary is accessed normally, the get method will not return an error. The returned value is None.
Has_key: Check whether the dictionary contains the given key. Expression d. has_key (k) is equivalent to k in d.
ItemsAndIteritems: Items returns dictionary items in the form of a list. iteritems works the same as items, but it returns an iterator object instead of a list.
KeysAndIterkeys: The keys method returns the keys in the dictionary as a list. The iterkeys method returns the values in the dictionary as a list.
Pop: Obtain the value corresponding to the given key and remove the key-value pair from the dictionary.
ValuesAndItervalues: Corresponds to keys and iterkeys. The values method returns the value in the dictionary in the form of a list (itervalues return value iterator)
Conclusion: we have learned the basic knowledge of strings, tuples, lists, and dictionaries in Python so far. How can we learn it next? If and while statements? I think these loop statements can be learned naturally in future Python examples. So next, I decided to continue learning about Python's advanced programming and crawler knowledge.
Come on! BaiYiShaoNian.