list) and can be sorted according to the key function>>> A.sort ()>>> A[' H ', ' e ', ' l ', ' l ', ' O ']>>> b = [' Hello ', ' Hi ', ' See You ']>>> b.sort (key = len)>>> b[' Hi ', ' hello ', ' See You ']Shard Assignment:>>> A[' H ', ' e ', ' l ', ' l ', ' O ']>>> A[1:3] = [3,4]>>> A[' H ', 3, 4, ' l ', ' O ']3, meta-groupTuples are immutable sequences, tuples cannot be modified; creating tuples is simple, and the value is a comma.>>> A = 3,>>> A(3,)>>> A =>>> A(1, 2, 3)The main factors to rem
is 001 The key is Age,value is-is-
Name,value is Zhangsan The key is Sex,value is
male
Method 2: sequence unpack the list of tuples returned by the dictionary items () method
>>> for Key,value in Dic.items ():
... print ' key is%s,value '%s '% (key,value) ...
The key is Id,value is 001 The key is Age,value is-is-
Name,value is Zhangsan The key is Sex,value is
male
If you are unfamiliar with lists, tuples, and sequences, it is be
This article describes the implementation of the Python two dictionaries (dict) of the method of merging, share for everyone to reference. The specific methods are as follows:
The existing two dictionaries are dict as follows:
DICT1={1:[1,11,111],2:[2,22,222]}
dict2={3:[3,33,333],4:[4,44,444]}
Merging two dict
Python dictionary key-Value Pair addition and traversal methods, python dictionary
Add a key-Value Pair
First define an empty dictionary
>>> Dic = {}
Directly assign values to keys that do not exist in the dictionary.
>>> dic['nam
can writePerson_info.keys ()#Show only KeyPerson_info.setdefault ('Gentle','male')#Set the default value, there is no change, no joinNew_info= {' Age': 19,'Addr': ['Wuhan','Beijing']}person_info.update (new_info)#Update the new dictionary to the old dictionaryPrintperson_info['name']#print is used in the file to outputPrintPerson_info.items ()#items default to the index and value (in the form of a list)PrintPerson_infoNote: The following are two type
Common Methods for operating the dictionary type in python (recommended) and for operating the dictionary type in python
The has_key () method can check whether the dictionary contains the specified key. If yes, True is returned. Otherwise, False is returned.
Syntax format:
In the Python manual:Sorted (Iterable[,cmp,[,key[,reverse=true]])Function: Return a new sorted list from the items in iterable.The first parameter is a iterable, and the return value is a list that sorts the elements in iterable. The optional parameters are three, CMP, key, and reverse. 1) CMP Specifies a custom comparison function that receives two parameters (elements of iterable), returns a negative number if the first argument is less than the sec
parentheses, of course, curly braces is a dictionary.★ There are 2 small details, first:If the list is only 1 digits, then it's OK, but tuple is using parentheses, in order for Python to know that you are here to define a tuple of 1 elements, a= (1,): A=[x.
>>> A=[1]
>>> a
[1]
>>> a= (1)
>>> A A
it's a number, not a tuple.
>>> a= (1,)
>>> a
(1,)
>>>
Second small detail: the invariant of a tuple refers to
'){1: ' t ', 2: ' t ', 3: ' t '}Update () #将两个字典的内容进行整合, if the consolidated dictionary key exists in the consolidated key during the integration process, replace the original value to ensure that there are no duplicate keys in the dictionaryScan two-dimensional code, Changbai Mountain Souvenir650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8D/E4/wKioL1iuilSRyP1mAAH4lkqQ0cE404.png-wh_500x0-wm_ 3-wmp_4-s_2600682946.png "title=" qq picture
2-1 How to filter data in lists, dictionaries, and collections based on criteria
Preliminary knowledge:In this section we will use the Randint,lambda,timeit,filter and other keywordsThe usual practice is to iterate through the current list and then place the element in the list that satisfies the condition in another listFrom random import RandintRandint (A,B) return random the integer in range [A, b], including both end points. use of the Timeit func
("abcdef", "123456")Print ("Alex Li". Translate (p))Replacement string: A_str.replace (' l ', ' l ', 1)Find character position in string from right to left: A_str.rfind (' x ')Separates a string into a list by a specified character, without specifying a space: A_str.split ()Separate strings by carriage return as a list: A_str.splitlines () (\r,\n?). )Case conversion: A_str.swapcase ()Fill in 0 of the string with insufficient parts: A_str.zfill (50)Dictionary7.18,7.19,7.21,7.28,8.28,2.28Key-valu
parameter list according to function F. For example:>>> L = [1,2,3,4,5]>>> map (lambda x:x*x, L)[1,4,9,16,25]The reduce (f, list) function is similar to map, whose argument f function receives two parameters, and all elements in the returned list are the result of the cumulative calculation of all elements in the parameter list according to function f, such as additive/multiplicative:>>> L = [1,2,3,4,5]>>> reduce (lambda x,y:x+y, l)15>>> reduce ( Lambda x,y:x*y, l)120Then using these two functi
引, can also not be specified, do not specify from the bottom to start deletingDel Name_list[i]Effect: spare tire and punctureHReverse () #列表反转Sort () #排序Remove ()Iv. GansoAOnly count () and index () are supportedFive, dictionaryAFavorite_languages = {' Jane ': ' Python ',' Jack ': ' C + + ',' Rose ': ' PHP ',' Jaymes ': ' Java ',}For I in Favorite_languages:Print (i)#默认输出keyBKey: Favorite_languages.keys ()Value: Favorite_languages.values ()Kv:favorite
Basic python Tutorial Study Notes --- (4) dictionary and python Study NotesSequence is a data structure in python, and ing is another. Mapping refers to a value by name. The only ing structure built in python is a dictionary. The
Today a friend gives a demand:
Come on.
{' IsOK ': 1, ' isrunning ': None, ' IsError ': none}
How to convert into a dictionary
Well, one look is that JSON conversion is simple and begins:
Live out not to result, also reported wrong, check two hours of Baidu, did not understand.
Finally, the direct copy of the online code, OK, run successfully, but put my a variable, no, error; Start comparing two variables what is different, always thought
Today a friend gives a demand:Come to
{' IsOK ': 1, ' isrunning ': None, ' IsError ': none}
How to convert into a dictionary
Well, the first thing to see is that JSON transformation is simple:
Import JSON
a = "{' IsOK ': 1, ' isrunning ': None, ' IsError ': none}"
print Json.loads (a)
Anyway out of the results, also reported wrong, check for two hours of Baidu, did not understand.
Finally, copy the code directly from the Web, OK, run s
Python combines dictionary key values and removes duplicate elements. python dictionary
Suppose there is a dictionary in python as follows:
X = {'A': '1, 2,3 ',' B ': '2, 3,4 '}
To merge:
X = {'C': '1, 2, 3, 4 '}
Three things need
The list in 1.python is not able to traverse delete1 #Python list We are not able to traverse delete2Li = [1, 2, 3, 4, 5, 6]3 forIinchRange (len (LI)):4 Print(i)5 delLi[i]6 Print(LI)7 #Output Result:8 #Traceback (most recent):9 #0Ten #[2, 3, 4, 5, 6] One #File "C:/users/xzb/pycharmprojects/s1/day7/python considerations. Py", line 5, A #1 - #del Li[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.