In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to sort the list in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The list is the most commonly used Python data type and can appear as a comma-separated value in square brackets. The data items of a list can be of different types, a string, a numeric type, or even a list, tuple, which is an element as long as it is separated by a comma.
An example of a list
1. Access elements in the python list
To access an element directly through an index, the basic format for accessing a single element is:
List name [index value]; the basic format for accessing multiple elements is: list name [index cause start value: index end value]
The following is an example:
Elements in the access list
Slicing of 2.python list
Slicing operations are not list-specific. Ordered sequences in python support slicing, such as strings and tuples.
The basic format is:
List name [index start value: index termination value: step size]
Where the step size is 1 by default
Examples of slicing of the list and corresponding output
List2= ["a", "d", "lily", "hello", 1pr 6]-this is a list of sources
List3=list2 [:]-the index value before and after omitting is a copy of the source list to get a new list
List3list4=list2 [2:]-the index termination value is omitted, which refers to the position from the initial value caused by the index to the final value
List5=list2 [: 3]-the omitted line causes the starting value, which refers to the position from the starting point to the end of the index
List6=list2 [1:4:2]-refers to a list of index values 1 to 3 with a step size of 2
Sorting of 3.python lists
The basic formula is:
List name .sort ()-sort the list in ascending order
List name .sort (reverse = True)-sort the list in descending order
List name .reverse-reverses the list
Sorting of python list and comparison of results
List= [1,3,5,8,2,0,6]
List.sort () sorts the list in ascending order
List1= [1,5,11,0,4,21,9]
List1.sort (reverse=True) sorts the list in descending order
List2= [1,5,11,0,4,21,9]
List2.reverse () inverts the list
It is important to note that the above method directly modifies the original list. If you want to keep the source list, you can use the sorted () method.
Sorted () method
Addition of 4.Python list elements
There are mainly the following ways:
List name .append (elements to be added)-this method can add only one element, usually at the end
List name .insert (location of elements to be added, elements to be added)-this method can be added anywhere
List name .extend (list)-extends the original list with the new list
Addition of Python list elements
Deletion of 5.Python list elements
There are mainly the following ways:
List name .pop ()-deletes the last element
List name .remove (elements to be deleted)-this method can delete elements anywhere
List name .clear ()-deletes all elements of the list
Del list name-Delete the entire list
Del list name [cause start value: index termination value]-Delete some elements in the list
This is the end of this article on "how to achieve list sorting in Python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.