Alibabacloud.com offers a wide variety of articles about dict comprehension python 3, easily find your dict comprehension python 3 information here online.
Part of the Source: http://blog.sina.com.cn/s/blog_5c6760940100bmg5.html① Direct Assignment----result is a reference to a different nameThe modification of the new dictionary has a full effect on the original dictionary, just a reference to the relationship>>> C = {'a': 1,'b': 2}>>> d =C>>> d['e']=3>>>d{'a': 1,'b': 2,'e': 3}>>>c{'a': 1,'b': 2,'e': 3}>>> f =
Python extracts dict and converts it to xml/json/table, and outputs the implementation code, dictjson
Core code:
#! /Usr/bin/python #-*-coding: gbk-*-# Set the source file output format import sysimport getoptimport jsonimport createDictimport myConToXMLimport myConToTable def getRsDataToDict (): # obtain the input parameters in the console and find the source da
shallow copy of a dictionaryDict.setdefault (Key, Default=none): Similar to get (), but if the key does not already exist in the dictionary, the key will be added and the value will be set to defaultCompared with list, Dict has the following features:
The speed of finding and inserting is very fast and will not increase with the increase of key;
It takes a lot of memory, and it wastes a lot of memory.
And the list is the opposite:
value is inside the list, use in, and return TrueIf the value exists, otherwise return to False .
Remove removes the first occurrence of a value from the list.
Pop is an interesting thing. It does two things: delete the last element of the list, and then return the value of the deleted element. Note that this differs from Li[-1] , which returns a value but does not change the list itself. Also differs from li.remove (value), which changes the list but does not return a value.
Lists can a
(RET1) result : True11,def pop (self, *args, **kwargs): Randomly deletes an element in the collection, returning the deleted element li = set ([1,2,4,6, ' s ', ' a ']) ret = Li.pop () print (LI) Results: {2, 4, 6, ' a ', ' s '} print (ret) Results: 112,def Remove (self, *args, **kwargs): Deletes the specified element in the collection, no return value Li = Set ([1,2,4,6, ' s ', ' a ']) Li.remove (2) print (LI) Result: {1, 4, 6, ' s ', ' a '}13,def symmetric_difference (self, *args, **kwargs): A
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
, for large lists, the extend executes faster.
Python supports the + = operator.Li + = [' both '] is equivalent to li.extend ([' both ']). The + = operator can be used for lists, strings, and integers, and it can also be overloaded for user-defined classes.
The * operator can act as a repeating device on the list. Li = [1, 2] * 3 equals to li = [1, 2] + [1, 2] + [1, 2], and three lists are connected
I saved some username, password, host, port, and other things related to database connection as a string in the database. I want to use MySQLdb to check the connection information of these databases, you need to pass the username, password, host, port, and other information in the database information to MySQLdb as parameters. connect (). In this case, '{"host": "192.168.11.22 Prime;," port ": 3306," user ":" abc "," passwd ":" 123
; variable name [key name]ValueAdd toDictionaries are added in different ways: Dictionary variable name [newly added key name] = value corresponding to new keyModifyDictionary modification format: dictionary variable name [to modify the value corresponding to the key name] = new valueDeleteThere are three common ways to delete a dictionary, and the function is different. The following is a brief introduction to the format of these methods, the specific role and skills of the method in-depth stud
Student = [{"No": 1,"score": 90},{"No": 2,"score": 90},{"No": 3,"score": 88},{"No": 4,"score": 92}]#single-level sorting, sorted by score onlystudent_sort_1= sorted (student, key=LambdaE:e.__getitem__('score'))#multilevel sorting, first according to score, and then by no sortstudent_sort_2= sorted (student, key=LambdaE: (E.__getitem__('score'), E.__getitem__('No')))Python list Nesting
First, ListList changes and additions#Increase,Classmates.append ("Nadech")#append an element at the end of theClassmates.insert (1,"Aguilera")#Insert at a location with an index value of 1#DeleteClassmates.pop ()#remove an element from the endClassmates.pop (1)#Delete an element from a location with an index value of 1#ChangeCLASSMATES[1] ="Aguilera"#CheckCLASSMATES[1]#viewing an element with an index value of 1CLASSMATES[-1]#View last elementOther Actions for list#returns the length of the lis
preferences. However, the Attrgetter () function typically runs faster and allows multiple fields to be compared at the same time. This is similar to the Operator.itemgetter () function for dictionary types (refer to section 1.13). For example, if the user instance also has a first_name and last_name property, you can sort the following:By_name = sorted (Users, Key=attrgetter (' last_name ', ' first_name '))It is also important to note that the techniques used in this section also apply to func
This article mainly introduced the writing Python script to convert the SQLAlchemy object to the Dict tutorial, mainly based on the Python model class constructs a transformation method, needs the friend to be possible to refer to under
When using SQLAlchemy to write Web applications, often use JSON to communicate, with JSON the closest object is
I have met once before, this time in the group also encountered a few times a problemA program written in python2.7, which uses a dictionary derivation, but the server version is python2.6, unable to run.Today, we checked the following about Dict comprehensions, which is clearly stated in pep274.http://legacy.python.org/dev/peps/pep-0274/Implementation All implementation details were resolved in the Python
', ' hehe3 '} #这种方式是直接定义一个集合List1 = {1, 2, 3, 4, 5, 6, 9}List2 = {2, 3, 4, 6, 1}List3 = {1, 2, 3}Print (List1.intersection (LIST2)) # Take the intersection, that is, take List1 and List2.print (List1 list2) # take intersectionPrint (List1.union (LIST2)) # takes the union, which is to merge List1 and List2, and then remove the duplicatePrint (List1 | list2) # FE
Python itself does not provide a switch syntax, and in order to solve problems like switch branch requirements, we can use dictionaries instead of implementations.Solution Ideas:
Handle the default in a switch statement using the fault tolerance of the Get method of the dictionary value
Set the Vlaue of the dictionary to the corresponding method name instead of the code block in the switch statement
Set the same value for different ke
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.