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
(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
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
.startswith ("Sylar") determines whether to start with SylarRet3 = S13.endswith ("Language?") Is it a ' language? ' EndRet7 = S13.count ("a") find the number of occurrences of "a"Ret5 = S13.find ("Sylar") find where ' Sylar ' appearsRet7 = S13.find ("A", 8, 22) cut to findRet8 = S13.index ("Sylar") Quest position. Attention. If the index is not found. Program will errorConditional judgmentis print (S14.isalnum ()) made up of words and numbers?is print (S14.isalpha ()) composed of words?Print (S1
Problem:Write a function most_prolific, which takes the same dictionary format as the beatles_discography example above and returns the year that the most albums were published. If you call this function in Beatles_discography, you should return to 1964, which is more than the number of albums issued in other years.From Builtins Import ListFrom collections Import CounterBeatles_discography = {"Please Me": 1963, "with the Beatles": 1963,"A Hard Day's Night": 1964, "Beatles for Sale": 1964, "Twist
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
List: Lists, data types can be differentDefinition: (example) classmates = [' Michael ', ' Bob ', ' Tracy ']Access to an element: (example) classmates[0] means access to the first element (example) classmates[-1] means access to the last elementGet list Length: (example) Len (classmates)Append elements: (example) classmates.append (' Adam ')Insert element to specified position: (example) Classmates.insert (1, ' Jack ')Delete End element: (example) Classmates.pop ()Delete the specified element: (
Di:print (Di[key]) Update Dict: Assign a value to the corresponding locationDelete dict:Dict Features:1: Fast Search speed2: Wasted Space3:key can not be repeated, and not variable4: Data disordered emissionsCreate set:s = set ([' Adam ', ' Lisa ', ' Bart ', ' gechong ', ' kongming ')Show set:s = set ([' Adam ', ' Lisa ', ' Bart ', ' gechong ', ' kongming ') ') if ' test ' in S:print (' Test ') else:print (' No ') Traverse Set:print (s)s = set (['
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
There are two methods in Python to determine whether a specified key value exists, one is judged by the method of the Dictionary object, and the other is through the in method, the following is a detailed example of Has_key.
123
d = { ' site ' : Code class= "py string" > ' www.cnblogs.com ' ' name ' : ' Cnblog ' , Code class= "py string" > ' object ' : ' Asgdjaa ' Code class= "Py plain" > #方法1: Via Has_key prin
collection is an unordered set of elements, and the result of the example declaration is:{' E ', ' C ', ' d ', ' B ', ' A '}The traversal method is:For I basket:Print (i)4. Dictionary DictDeclaration Example: tel = {' Jack ': 23432, ' scape ': 234}The following methods can be used to assign values: tel[' chunyu '] = 19910805The result is: {' Chunyu ': 19910805, ' Jack ': 23432, ' scape ': 234}You can use the items () method to get the keys and corresponding values, for example:For k,v in Tel.it
', ' 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) # FETCH and setPrint (List1.difference (list2)) #取差集 exist in list, not in List2print (LIST1-LIST2)Print (List3.issubset (list1)) #判断list3是
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.