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 .
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 .
Used to store elements in the list in reverse .
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 .
Used to sort the original list ( Sort ascending by default ), The sorted new list will overwrite the original list .
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 .
And sort() The method is different , Built in functions sorted() Return to the new list , No changes are made to the original list .
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 .
1、 Liao Xuefeng's official website 2、python Official website 3、Python Programming case tutorial
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 .