Chapter 5-dict objects
Similar to the map of C ++ STL in Python, dict is a ing container (key-> value), but the implementation principle is different. Because python uses a large number of structures such as dict (such as the Intese mechanism of string objects) and has high
The Python dictionary has three constructors, Dict (), Dict (**args), dict (mapping), and the first and second constructors are better understood and easier to use.The use of the dict (mapping) constructor is more difficult to understand.1
The knowledge shared in this article is the basic concept, common operations, and examples of the Dictionary (dict) in Python data types. It is very useful for you to understand the dictionary dict, I hope you will like it.
I. Basic Data Types
Integer: intString: str (Note: \ t is equal to a tab key)Boolean value: boolList: listList []Ancestor: tupleAncestor use
3, Del d[' a '] #删除键值为 ' a ' element
Traversing elements
For K in D:
print ' d[%s]= '% k,d[k]
Or
For k,v in D.items ():
print ' d[%s]= '% k,v
Or
For k,v in D.iteritems ():
print ' d[%s]= '% k,v
Or
For k,v in D.viewitems ():
print ' d[%s]= '% k,v
Items (), Iteritems () and viewitems () differences
Python2.x's items () is a list that contains all the elements of dict, like the one above, but because of this too much memory, it was later added (note:
Python built-in dictionary: dict support, Dict full name dictionary, also known as map in other languages, using key-value (Key-value) storage, with extremely fast search speedUse Help (Dict) to find dict-related function actionsFor example: dt =
Many of the dict methods are similar to the list, with one by one below and a comparison with the list.
Nesting
Nested in the list also exists, that is, the element is a list, in the dict, there are similar styles:
>>> a_list = [[1,2,3],[4,5],[6,7]]>>> a_list[1][1]5>>> a_dict = {1:{"name": "Qiwsir"},2: " Python "," email ":" qiwsir@gmail.com "}>>> a_dict{1: {'
A dictionary is the only type of mapping in the Python language. A hash value (key, key) and a pointing object (value, value) in a mapped type object are a one-to-many relationship that is often considered a mutable hash table. The object of the dictionary is mutable, which is a container type that can store any number of Python objects, which can also include other container types. The data in the dictiona
Note: When none is returned, the interactive command line of Python does not display the results. To delete a key, use the Pop (key) method, and the corresponding value will also be removed from dict: >>> d.pop (' Bob ') 75>>> d{' Michael ': Up, ' Tracy ': 85} Be sure to note thatThe order in which the dict is stored inside is not related to the order in which th
In Python, set represents the collection, list stands for lists, dict represents the dictionaryThe difference between set and Dict is that the dict is a storage key-value, each key is unique, the set is stored as a key relative to the dict, and the key is unique, and the lis
The following small series will bring you a python dictionary (dict) key and value sorting. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you a python dictionary (dict) key and value sorting. I think this is quite good. now I
Python's nested collection types are list, tuple, set, Dict.
List: Looks like an array, but more powerful than an array, supports indexing, slicing, finding, adding, and so on.
Tuple tuples: Functions are similar to lists, but once generated, the lengths and elements are immutable (elements are mutable) and seem to be a more lightweight, secure list.
Dictionary dict: Key-value pairs structure hash table, a
ordered collection with Dict.The third feature of Dict is that elements that are key must be immutable, and the basic types of python, such as strings, integers, and floating-point numbers, are immutable and can be used as keys. But the list is mutable and cannot be a key.What errors will be reported when trying to try list as key.Immutable this limit is only used for key,value whether the variable does no
The value of the iteration Dict1:values ()2:itervalues ()3:forkey inDi:print(Di[key])We have learned that the Dict object itself is an iterative object , iterating the dict directly with a for loop, and can get a key for dict each time.What should we do if we want to iterate over the value of the Dict object?The
Dictionary full Name (dictionaries)字典为非线性结构类似Dict称为k-v对、全名为:key - value键值对的数据的集合dict是可变的、无序的、key不重复(去重)*key必须可以hash*set可称为dict的简化版dict是一个个kv对,其中k一定不相同,keys可以当set用、如果value可hash,那么items可以当set用Dictionary dict definition Initialization d = d
does not matter:{' 123 ': [1, 2, 3], # key is Str,value is List123: ' 123 ', # key is Int,value is str(' A ', ' B '): True # key is a tuple, and each element of a tuple is an immutable object, value is a Boolean}The most commonly used key is also a string, because it is most convenient to use.Two. Update DictDict is variable and can be added at any time in the network Dict Key-valueD = {' Adam ': 95,' Lisa ': 85,' Bart ': 59}Add a new classmate ' Pau
Python, with its graceful syntax and handy built-in data structure, has won the close gaze of many programmers.One of the most useful data structures is the dictionary (dict), which is very simple to use. When it comes to traversing a dict structure, I think most people will think about the method for the key in Dictobj, which is true for most cases. But not comp
This article mainly introduces the application details of python dict, set, list, And tuple. For more information, see this article, the tuple application and corresponding examples help readers understand its concepts and principles. The details are as follows:
1. Dictionary (dict)
Dict surrounded {}
Many of Dict's methods are similar to the list, with one by one of them coming in, and they will be compared to the list.
Nesting
Nesting also exists in the list, where elements are list, and in Dict, there are similar styles:
>>> a_list = [[1,2,3],[4,5],[6,7]]
>>> a_list[1][1]
5
>>> a_dict = {1:{"name": "Qiwsir"},2: "python", "email": "qiwsir@gmail.com"}
>>> a_dict
{1: {' name ': ' Qiwsir '}, 2:
Python has won a lot of programmers ' pro-gaze with its beautiful syntax and convenient built-in data structures.
There is a very useful data structure, which is the dictionary (dict), which is very simple to use. When it comes to traversing a dict structure, I think most people will think of the for-key in Dictobj method, and it is true that this method is appli
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.