List.sort () method
This function method makes a forward sort of the list contents, the sorted new list overwrites the original list, and the sort sort method directly modifies the original list
A = [5, 7, 6, 3, 4, 1, 2]
A.sort ()
Print a
A.reverse () # Reverse
Print a sorted () method
That is, you can keep the original list and get the sorted list.
A = [5, 7, 6, 3, 4, 1, 2]
b = Sorted (a)
Print B
c = Sorted (a,reverse=1) #倒序
The difference between print C
Sort () is a method of a Mutable object (dictionary, list) with no arguments, no return value, and sort () that changes the Mutable object, so no return value is required.
The sort () method is a method or property that is unique to a mutable object, and as immutable objects such as tuples and strings that do not have these methods, if the call returns an exception.
Sorted () is a built-in function of Python, not a unique method for mutable objects (lists, dictionaries), sorted ()
The function requires a parameter (the argument can be a list, dictionary, tuple, string), regardless of the arguments passed, returns a value in the list as a container, if the dictionary returns a list of keys.
The function requires a parameter (the argument can be a list, dictionary, tuple, string), regardless of the arguments passed, returns a value in the list as a container, if the dictionary returns a list of keys. The application of lambda function in tuple and dictionary sorting
a= [(' B ', 3), (' A ', 1), (' C ', 2)]
b = sorted (A,key = lambda a_tuple:a_tuple[0]) #按第一个元素排序
Print B
c = sorted (A,key = lambda a_tuple:a_tuple[1]) #按第二个元素排序
Print C
D = sorted (A,key=lambda a_tuple:a_tuple[0],reverse=1) #按第一个元素倒序
Print D
object property Sorting
Define class:
Class locus:
#定义类属性, which is a property of a class object that is owned by an instance object of all class objects, and only one copy exists in memory.
#类似静态成员变量, for common class attributes, can be accessed by class objects and instance objects outside the class.
Version = 1.0 #公有的类属性
__x=0 #如果在属性名前面加了2个下划线 ' __ ' indicates that the property is a private property and cannot access private class properties outside of the class through the class object
#构造方法
def __ Init__ (Self,commaddr,timestr,status,lon,lat):
self.commaddr = commaddr
self.timestr = timeStr
Self.status = status
Self.lon = Lon
Self.lat = Lat
#返回私有属性值
def GetX (self): return
self.__x< c16/> #返回字符串, like ToString ()
def __str__ (self) in Java: Return
self.commaddr+ ", +self.timestr+", "+ Self.status+ "," +self.lon+ "," +self.lat
Sorting algorithm:
Locuslist = []
L1 = locus ("A", "20141015023258", "1", "116.4532242", "39.9476242")
L2 = Locus ("23", " 20141015023244 "," 1 "," 116.4532242 "," 39.9476242 ")
L3 = Locus (" "," 20141015023257 ", " 1 "," 116.4532242 "," " 39.9476242 ")
locuslist.append (L1)
locuslist.append (L2)
Locuslist.append (L3) LocusList1
= sorted ( Locuslist,key = lambda locus:locus.timeStr) #按timeStr排序 for
locus in LocusList1:
print locus to
I in range ( 0,len (LocusList1)):
print Locuslist1[i].timestr
Output results:
23,20141015023244,1,116.4532242,39.9476242
23,20141015023257,1,116.4532242,39.9476242
23,20141015023258,1,116.4532242,39.9476242
20141015023244
20141015023257
20141015023258