程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Sorting of Python list basic operations

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .


One 、 background

Python Is an easy to learn 、 Powerful programming language . It provides an efficient high-level data structure , It's also a simple and effective way of object-oriented programming .Python Elegant grammar and dynamic typing and the essence of interpretive language , Make it an ideal language for scripting and rapid application development on most platforms . Now let's introduce python The syntax for sorting lists .


Two 、 Sort the list

1、reverse() Method

Used to store elements in the list in reverse .

  • list Represents a list , The method has no parameters , no return value

list.reverse()

example : Find elements in the list , If you find , Output the index position of the element in the list , Otherwise, the output is not found .

x = [1, 2, 3, 4] # Create a list and assign values
x.reverse() # Use reverse() Method will change the variable x The elements in are stored in reverse
print(x)

give the result as follows .

2、sort() Method

Used to sort the original list ( Sort ascending by default ), The sorted new list will overwrite the original list .

  • list Represents a list
  • key Is an optional parameter , If the parameter is specified , This parameter's method is used for sorting
  • reverse Is an optional parameter , Indicates whether to reverse sort , The default is False

list.sort([key=None][,reverse=False])

example 1: Given arbitrary n It's an integer , Sort them from small to large , And output the result .

x = [3, 2, 1, 5, 4] # Create a list and assign values
x.sort() # Use sort() Method on variable x Sort the elements in ascending order
print(x) # Output list x

give the result as follows .

example 2: Sort multiple strings in reverse order according to their length and output .

x = [3, 2, 1, 5, 4] # Create a list and assign values
x.sort() # Use sort() Method on variable x Sort the elements in ascending order
print(x) # Output list x

give the result as follows .

3、sorted() Method

And sort() The method is different , Built in functions sorted() Return to the new list , No changes are made to the original list .

  • iterable Represents an iteratable object , Here is the list name
  • Parameters key and reverse The usage and sort() Same method in

sorted(iterable[,key=None][,reverse=False])

example 1:

x = [1, 5, 2, 3, 4] # Create a list and assign values
y = sorted(x) # take x The elements in are sorted in ascending order and assigned to variables y
print(x)
print(y)

give the result as follows .


3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial


Four 、 summary

The above is about python The syntax for sorting lists , You can refer to it , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved