Introduction to the Python sorted function

Source: Internet
Author: User
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 1
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]) Topic 2 answers

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 using A<b, I will automatically call the __ln__ (A, B) function, so we want to redefine the __LN (A, A, a) __ function in the class, and define when to return true when it returns false. Each type has its own ln () function, so it can also be called at the time of redefinition.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.