,' Jcodeer ']print L#输出结果: [1, 2, 3, 4, ' Jcodeer ']#于list直接打印即可" ' 6. Out dictionary (dictionary) '{1:' A ',2:' B ',3:' C ',4:' d '}print D #输出结果: {1: ' A ', 2: ' B ', 3: ' C ', 4: ' D '}#同Python is also supported by dictionary
The example of this article summarizes the common operation methods of Dict dictionary in Python. Share to everyone for your reference. Specifically as follows:
The following Python code shows the common operation of the dictionary in Python, the
#author: Zhouyu#Dictionaries, which are often used in Python, are stored in Key:value,# It's unordered, unlike our dictionary, the dictionary is enclosed in curly braces {}A= {' Name ': ' Zhouyu ',' age ':22,' sex ': ' Male ',' QQ ': ' 1090139534 '}#打印出字典Print(A)#打印指定的key的valuePrint(A[' name '])#修改keyA[' name '] = ' Zh
is ZY"You can also use single quotesSTR = 'My name is ZY'You can also use three quotation marks.STR = ''' sent to and sent to non '''
Other methods can be seen through help (string). I will not repeat it.Here are some special usage instructions.String. startswith (STR): indicates whether the string starts with a certain string. If yes, true is returned.String. Find (STR): searches for a string in the string and returns the true result.If 'str' in name: This is another way of writing the above
Python uses the item () method to traverse the dictionary.
There are several methods to traverse the Python dictionary. One of them is for... in, which I will not explain. It is almost everywhere in Python. The Traversal method described below is the item () method.
Item ()
This article mainly introduces the dictionary traversal memo in Python. This article lists a variety of dictionary traversal methods, which are suitable for beginners to view and briefly explains the differences between interitems and iterms, for more information, see how to traverse the dictionary in
Basic composition and usage of the Python dictionary
#! /Usr/bin/env python#-*-Coding: UTF-8 -*-"""Old Rules: Environment 2.7.x in the following method. Please remember the print format (the output content should be placed in parentheses) for users of version 3.x or later)Basic composition and usage of dictionariesDict
'}If you have this primary key, change the element of age to 13
Del Dict[x]
Delete the element, delete the element only need to delete the corresponding component to delete the element, when the deleted element is not in the dictionary, return error
a = { ' name ' : " DEMONLG ' , ' 123131 ' : , ' 32313 ' : " 2222 ' , ' 4343 ' : "} del a[ ' 1233 ' ] print (a) File "e:/
are very similar, support negative index, Shard, multivariate listnames=['Michale','Bob','TRACY'] forIinchnames:Print(i) Note: Circular output List Names=('Michale','Bob','TRACY') forIinchnames:Print(i) Note: cyclic output tuples(2) Find, sort, reverseFind (2 ways). The index method returns the position of the element in the list, and "in" to determine whether the element is in the list.List = ['Apple','Banana','Grange','Orange','Anguo','C + +']Print
Today, the Python dictionary has been specifically tested for performance results in comparison with various methods.The test code is as follows:1 defdict_traverse ():2 fromTimeImportClock3My_dict = {'name':'Jim',' Age':' -','Height':'180cm','Weight':'60kg'}4 5T_start =clock ()6 forKeyinchMy_dict:#worst-performing notation. No optimization7 Print
;> print (t) (1, 2, 'A') >>> d = {'A': 1, 'B': 2 }# dictionary >>> print (d) {'A': 1, 'B': 2}
2. format the output integer
Python string formatting symbol:
Letter Number
Description
% C
Format characters and their ASCII codes
% S
Format a string
% D
Form
present in the dictionary, it is assigned a value by Dict[key]=default.
Dict.setdefault (Key,default=none)
Similar to the method set (), if the key is not present in the dictionary, it is assigned a value by Dict[key]=default.
Practice:
Shopping Cart Program requirements:
1. After starting the program, let the user enter the salary, and then
#!/usr/bin/python#Dictionary#While learning Java, the Language foundation for a long time, and then a variety of APIs, and finally the collection#Key-value pairs, variable#1. Mapping OperationsD= {' Food':'Spam','Quantity': 4,'Color':'Pink'}Print(d[' Food'])#to get a value by keyd['Quantity'] + = 1#change the value corresponding to the key#another way to construc
Here is an easy way for you to iterate through the dictionary elements in a python loop. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
A simple for statement can loop all the keys of a dictionary, just as you would with a sequence:
In [1]: D = {' X ': 1, ' Y ': 2, '
attributeerror: ' Dict ' object have no attribute ' Iteritems ' >>> VI = d.viewitems () Traceback (most recent call last): File "
", line 1, in
attributeerror: ' Dict ' object has no attribute ' viewitems ' >>> il dict_items ([' Size ', ' large '), (' Q Uantity ', 6)])
As you can see in Python 3.x, both Iteritems () and Viewitems () have been abol
Python Meta DictionaryThe Dictionary (dictionary) is the most flexible built-in data structure type in Python, except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.The difference between the two is that the elem
don't have this keyDic.get (key, return value) View 2: None returns none, specifying what to returnDic.setdefault (Key) View 3: No then return none1DIC = {"K1":"v1","K2":"v2","K3":"v3"}2 forIinchDIC:3 Print. IO4 Print(dic["K1"])5 Print(Dic.get ("K2"))6 Print(Dic.setdefault ("K3"))3. Other operations of the dictio
Use the setdefault () method for dictionary operations in Python
This article mainly introduces the use of the setdefault () method for dictionary operations in Python. It is the basic knowledge of getting started with Python. For more information, see
The setdefault () meth
The dictionary is Python's only innuendo type hash>>> brand = [' Li ning ', ' Nike ', ' Adidas ']>>> slogan = [' Everything is possible ', ' Just do it ', ' impossible are nothing ']>>> print (' Li Ning's slogan is: ', slogan[brand.index (' Plum ')])Li Ning's slogan is: everything is possible Dictionary is not a sequence type, it is a mapping typeString list tupl
Python Dictionary (dict) Key and value sorting, pythondict
The characteristics of the python Dictionary (dict) are unordered. The corresponding values are extracted by key. If we need to sort the dictionary by value, you can use the following method:
1. Values are sorted i
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.