This article mainly introduces how to add entries to a dictionary using python. it is a practical dictionary operation technique and provides simple annotations for examples, for more information about how to add a dictionary entry using python, see the following example. Sh
" Print (list2) print (LIST3) #根据id可知, the assignment operation of the dictionary creates a label that points to the # data, not a true copy a={"name": "The Little Turtle "}b=aprint (ID (a)) print (ID (b)) print (b) #将a指向了空字典, instead of changing the size of its data # here
delete a single element also clears the dictionary, emptying only one operation.Show Delete a dictionary with the Del command, as in the following example:#!/usr/bin/pythonDict= {'Name':'Zara',' Age': 7,'Class':' First'};deldict['Name'];#Delete key is ' Name ' entryDict.clear ();#Empty dictionary all entriesdelDict;#Delete DictionaryPrint "dict[' age ']:", dict[
This article mainly introduces the Python dict dictionary structure operation method learning notebook. The dictionary operation is the basic knowledge in the Python getting started learning. For more information, see
I. Basic dictionary Methods
1. Create a
What is a dictionary?
A dictionary is the only type of mapping in the Python language.
A hash value (key, key) and a pointer to an object (values, value) are a one-to-many relationship that is often considered a mutable hash table in the mapping type object.
The Dictionary object is mutable, which is a container type
' > >> dict.setdefault (' age ', ' a ') 18>>> dict{' Color ': ' White ', ' name ': ' Tom ', ' age ': ' Hair ': ' Black '}Pretty PrintIf you import the Pprint module in a program, you can use the Pprint () and Pformat () functions, which will print beautifully a dictionary of words.The Pprint.pprint () function is especially useful if the
unique
? B. The key in the dictionary must be immutable
? List is mutable and cannot function as key
? A tuple is immutable and can act as a key, a numeric type, and a string can act as key
2. Create:
syntax: dictionary name = {key1:value1,key2:value2 ...}
Code Demo:
#1. Creating a Dictionary
#创建一个空字典
Dict1 = {}
#创建有键值对的字
(List2)) #对称差集, which takes a value that is not shared by both parties print ("--- -------") Print (List2.isdisjoint (LIST4)) #判断是否有交集print (List1 List2) #交集print (List2 | list1) #并集print (list1-list2 ) #差集print (list1 ^ list2)
One, what is a dictionary?
Dictionaries are the only mapping types in the Python language.
The hash value (key, key) and the object (value) in the mapped type object are one-to-many relationships and are often considered to be variable hash tables.
A Dictionary object is variable and is a container type that can store any number of
; Dict.fromkeys (L1, ' over '){1: ' over ', 2: ' over ', 3: ' over '}>>> D{3: ' z ', ' x ': 1, ' y ': 3}>>> d[3]' Z '>>> d[' x ']1>>> d[0]Traceback (most recent):File "d[0]keyerror:0>>> d = {' z ': 5, ' x ': 1.5, ' y ': 3}>>> d.items ()dict_items ([' z ', 5), (' x ', 1.5), (' y ', 3)])>>> list (d.items ())[(' z ', 5), (' x ', 1.5), (' y ', 3)]>>> D.keys ()Dict_keys ([' z ', ' x ', ' y '])>>> for x in D.keys ():Print (x)Zxy>>> d1 = {' x ': 1, ' y ': 3}
values in a dictionary
To traverse a dictionary (the General key), you only need to loop through its keys, like this:
The code is as follows
Copy Code
>>> dict2 = {' name ': ' Earth ', ' Port ': 80}>>>>>>> for key in Dict2.keys ():... print ' key=%s, value=%s '% (key, Dict2[key])...Key=name, Value=earthKey=port, value=80
S
to associate the value V on the key KDel D[k] Delete key to KK in D check if D contains keys to KDescription
Key type: The key of the dictionary is not necessarily integer data, or it may be other immutable types
Auto Add: Even if the key doesn't exist in the dictionary, it can be assigned a value so that the dictionary creates a new item and cannot associate
data type, such as a string, a number, or a tuple.
To access the value of a dictionary:
To access the dictionary element, you can use the familiar square brackets together to get the key to its value. The following is a simple example:
?
1 2 3 4 5 6
#!/usr/bin/python dict = {' Name ': ' Zara ', ' age ': 7, ' Class ': ' A ';
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 Pyt
': ' Linuxzen '}
The Python dictionary Update method can use a dictionary to update the dictionary, or you can update a dictionary in the same way as the Dict function by passing parameters like the one in the code, but the second is more elegant, but similar to the
traversed (key, value) tuples as a list. Keys ()#returns a dictionary of all keys in a list>>> D1 = {'Addr':{'IP': 127.0.0.1,'Port': 80},'msg': 18}>>>D1.keys () Dict_keys (['msg','Addr']). VALUES ()#returns all values in the dictionary as a list>>> D1 = {'Addr':{'IP': 127.0.0.1,'Port': 80},'msg': 18}>>>d1.values () dict_values ([19, {'IP':'127.0.0.1','Port': 80}]). SetDefault (key, default=none)#similar to
Python mapping--Introduction to dictionary DictionariesDictionaries, as a kind of Python container, do not need to be ordered, so dictionaries are not part of the sequence. In this dictionary there are key (keys) and values (value). The value is stored in the key, so we can derive the value as long as we access the key
Solution to the exception caused by changing elements during dictionary traversal in Python
Let's take a look at some basic methods for traversing the dictionary in Python:Script:
#!/usr/bin/python dict={"a":"apple","b":"banana","o":"orange"} print "##########dict##########
Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
Through our basic tutorial, we have established basic concepts for python and have a relatively clear understanding of objects and classes.
Our advanced tutorial is to further expand the basic tutorial and learn more about Python. I hope that you will have a comprehensive understanding of t
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.