1. Dictionaries1.1 The difference between a dictionary type and a sequence type:Data is accessed and accessed in different ways.The sequence type is indexed only by numeric type keys (numerically sequentially from the beginning of the sequence);A mapping type can be used as a key for other object types (such as numbers, strings, ganso, usually strings as keys), and the keys of the sequence type are different, and the key of the mapping type is directl
dictionary ispythonThe unique mapping type in the language, with curly braces{}indicates that a dictionary entry is a key-value pair, and the methodkeys ()returns the list of keys for the dictionary,values ()returns a list of values for the dictionary,items ()returns a list
'))#similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to defaultPrint('Dict1.setdefault (key):', Dict1.setdefault ('Sex', None))Print("dict1.setdefault (' sex '):", Dict1) Dict1={'name':'SJL',' Age': 20,'Address':'Xian'}dict2={'Sex':'Girl'}dict1.update (DICT2)#update the key/
(or none returned).5. Get Dictionary ContentPrint (D.values ()): Prints all valuesPrint (D.keys ()): Prints all keysPrint (D.items ()): Prints all keys and corresponding valuesPrint nested dictionaries: print (first dictionary name [' Second dictionary '] [' Third dictionary
This article mainly introduces the basics of python, including file input and output, built-in types, dictionary operations, and other usage methods.
I. variables and expressions
The code is as follows:
>>> 1 + 12>>> Print 'Hello world'Hello world>>> X = 1>>> Y = 2>>> X + y3
Python is a strongly typed language and cannot be automatically parsed and converted
dictionaries , unordered collections in python, key-value structures, access values by key, rather than relying on offsets or indexes to access Them. Here are some of the basic properties of the Dictionary:
The value of a dictionary can be any
;> C{' names ': [' Alfred ', ' Bertrand ', ' Clive ']}>>> D{' names ': [' Alfred ', ' Bertrand ', ' Clive ']}>>> DC{' names ': [' Alfred ', ' Bertrand ']}3. Fromkeys Create a new dictionary with the given key>>> {}.fromkeys ([' Name ', ' age ']){' Age ': none, ' Name ': none}>>> Dict.fromkeys ([' Name ', ' age ']){' Age ': none, ' Name ': none}>>> Dict.fromkeys ([' Name ', ' age '], ' (unkown) '){' Age ': ' (unkown) ', ' Name ': ' (unkown) '}4,
key cyclically, as shown in the following code:
>>>dict2={'name':'earth','port':80}
>>>
>>>>forkeyindict2.keys():
...print'key=%s,value=%s'%(key,dict2[key])
...
key=name,value=earth
key=port,value=80
② Traverse the dictionary in the for loop from Python
0x 01 Dictionary IntroductionThe child and list are the two most common data types in Python, the dictionary is the data type of the key-value pair (key-value) format, it has the same index as the list, but not the subscript in the list, but the key is used as the index, the
the initial value corresponding to all keys in the dictionary
4
Radiansdict.get (Key, Default=none)Returns the value of the specified key if the value does not return the default value in the dictionary
Mappings : Relationship of key-value pairs, key (key) mapping values (value)The dictionary is the only type of mapping for Python>>> Phonebook = {'Wakey':'1111','Ethon':'2222','Joho':'3333'}>>>phonebook{'Joho':'3333','Wakey':'1111','Ethon':'2222'}Extension: the difference between a mapping type and a sequence type1, Ac
"value=%s"% (value)the difference between values and viewvalues hereThe latter returns a View object of the dictionary, similar to the view in the database, when the dict changes, the View object changes as well .Common MethodsPython codeD = {"Name": "Nico", "Age": 23}d["name"] = "AAAA"d["Address"] = "ABCDEFG ...."Print d #{' age ':, ' name ': ' AAAA ', ' addres
. If the key does not exist, a new key-value is created.
# Delete
Infos. pop ('name') # specify the key to delete the key and the corresponding value.Infos. popitem () # randomly delete a key and the corresponding valueDel infos ['phone'] # specify the key to delete the key. If the key exists, the key and value are deleted. If the key does not exist, an error is returned.Infos. clear () # clear 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}
This article mainly introduced the Python dictionary Sort implementation method, the example analyzes the Python dictionary sorting correlation skill, needs the friend to refer to the next
This paper analyzes the method of Python dictio
Python uses the zipper method to implement the dictionary method example, python zipper
Preface
A dictionary is also called a discrete list. The greatest feature is that it uses keys to find its corresponding values. Its time complexity is O (1 ), the following article introduces how to implement the
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Create a dictionary
>>> phonebook={'Alice':'2897','Al
If the dictionary is dic={' name ': Barbie, ' age ': 20}, the Dic.name in HTML is barbie,dic.age to 20.But if the dictionary is dic={' Barbie ': 1, ' Roger ': 2, ' Kitty ': 3}, to use the variable name in HTML to represent the name, you cannot get the corresponding value directly with Dic.name.Official Document: https:
,python 3.0 does not have the functionprint ' Has_key ', D.has_key (' Mali ')#7 the. Items method returns all dictionary items as a list, each of which comes from (key, value)d={' Mali ': +, ' Shasha ': 85}Print D.items ()The #iteritems method works roughly the same, but returns an iterator object instead of a listIt=d.iteritems ()Print itPrint List (IT)#8 the. K
The dictionary in Python belongs to an advanced data structure, which is based on the hash table, and the complexity of the hash table in finding an element is basically O (1), so it is a very useful data structure. The following is a brief introduction to the operation of some dictionaries of capitalCreate a dictionary, assign a
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.