The first two days to learn the socket programming, in the big God to ask a question was rejected, there is a kind of have not learned to walk just want to run feeling. The great God said that my current level should be to do some like the operation of files, serial numbers and other small exercises to deepen understanding. Here's a little exercise he gave me:
1. datas = [' Sherry ', ' female ', '],[' flora ', ' Female '],[' June ', and ' femal '], respectively, according to the first letter and age to sort the output;
2, according to the output of the output method to compare the results, the person class to supplement;
Class_mates = {' Sherry ': [, ' Male '], ' June ': [+, ' female '], ' flora ': [+, ' female '], ' Alina ': [+] ' male ']}class person ( Object): Def __init__ (self,name,age): Self.name = NAMEP1 = Person (' sherry ', ') P2 = person (' June ', ') if P1<p2:print (' p 1:{} less than p2:{} '. Format ([p1.name,p1.age],[p2.name,p2.age])) Else:print (' p1:{} GTE than p2:{} '. Format ([P1.name, P1.age],[p2.name,p2.age]))
It's so simple I've been doing it all afternoon (face)
def get_first (info): first_value = info[0][0] return first_value na = sorted (datas,key=get_first) print (NA) def Age_sort (Info): return info[1] Print (sorted (datas,key=age_sort))
Topic 2 Answers
Class_mates = {' Sherry ': [, ' Male '], ' June ': [+, ' female '], ' flora ': [+, ' female '], ' Alina ': [+] ' male ']}class person ( Object): def __init__ (self,name,age): self.name = name Self.age = Age def __lt__ (self,others): if (self.age<others.age): return 1 elif (self.age==others.age): if (self.name[0]<others.name[0]) : return 1 else: return 0 return 0p1 = person (' Sherry ', "p2") = Person (' June ', ') if P1<P2: Print (' p1:{} less than p2:{} '. Format ([p1.name,p1.age],[p2.name,p2.age])) Else: print (' p1:{} GTE than p2:{} '. Format ([p1.name,p1.age],[p2.name,p2.age]))
Check out the official Python documentation to summarize the knowledge of get.
1、sorted
(iterable[, key][, reverse])
Returns a reordered list with two optional keyword parameters (specifying parameters using the parameter name instead of the location).
key defines a function with parameters, extracts a list of an element as the function's parameters, the return value as you call the keyword, the default value is None (directly compare the elements of the list)
Reverse is a Boolean value. True indicates that the elements inside the list are sorted backwards.
2, ln (A, b), when I use A<b, I automatically call ln (a, B) function, so we want to redefine the ln (a, B) function in the class, and define when it returns True when it returns false. Each type has its own ln () function, so it can also be called at the time of redefinition.