Sort columns (list)

Source: Internet
Author: User

Sort

Method One

1. Sorting by numbers (from small to large, changing itself)
scores = [23,43,54,67,87,100]  scores.sort()  print(scores)  
2. Sorting strings (alphabetically)
**方法一 .sort()**      name = ["shi","sheng","ru","yi","huang","meng"]      name.sort()      print(name)  **方法二 sorted()函数(返回一个新的结果,列表的本身不会变化)**      a = [34,1,43,99,78]    a = ["shi","sheng","ru","yi","huang","meng"]      print(sorted(a))      print(a)  
3. Sorting strings (sorted by end letter)
name = ["shi","sheng","ru","yi","huang","meng"]    name.sort(key=lambda a:a[-1])  print(name)  
Inverted list
**方法一**    scores = [23,43,54,3,87,100]    scores.sort(reverse=True)   #(先从大到小排序再倒置)    print(scores)    **方法二**      scores = [23,43,54,3,87,100]    scores.reverse() #(直接倒置)    print(scores)  

Comments:
1. Sort the list. Sort ()
2. If it is a letter, sort it by the first letter
3. If you want to sort by the last letter, list. Sort (Key=lambda a:a[-1])
4. List of inverted sequences. Sort (reverse=true)
5. Inverted sequence list. Reverse ()

sorted() 函数    按照从小到大进行排序,会返回一个新的结果,本身不会发生变化

Sort columns (list)

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.