Silly and unclear sort, sorted, reverse, reversed

foreword


In the usual coding process, lists are often used, and the common method is basically to traverse the loop to append elements. There are still many methods that I am not familiar with. For example, I once encountered a problem: reverse a list?

Pick up Baidu and get the answer:

Method 1: list slicing, step size is set to -1

Method 2: The list has its own method: lst.reverse()

However, this is just the beginning. I hurriedly checked all the methods of the list. There are two methods, sort and reverse, which remind me of the similar functions of sorted and reversed. After some research, I finally figured it out

In the process of understanding the sort, reverse method and sorted, reversed function, I thought of the following two concepts. Personally, the understanding of these two concepts is helpful for today's content get

A commonplace: object-oriented and process-oriented programming ideas

Personal understanding is for reference only:

Python is an object-oriented development language. Based on an object, which properties and behaviors it has, it is packaged as a whole. In fact, a list is also an object. Python currently supports list operation methods such as: append, insert, sort, reverse, which is the behavior that list objects can perform, that is, object methods

Separate functions, such as sorted and reversed functions, can be understood as a process-oriented programming, which implements a process, then a certain object needs to use this process, and then deal with the object, for example: in java learning, use Introduce the example of starting and stopping the vehicle, using process-oriented programming, which is to define the starting and stopping process. Different objects include the starting process, such as: cars, large trucks, and they will go through this process if they want to run. It’s just that different start-up processes are made according to the type of incoming vehicle, and object-oriented is to encapsulate the start and stop process of the car in a class. As long as it is a vehicle class, it will have such behavior, and then different objects can be Call a specific object method. The difference is: one focuses on the process, and the other focuses on the object

Re-understand object-oriented and process-oriented, and then look at the sort method and sorted function; reverse method and reversed function

(1) sort method and sorted function: sorting uses

①List sort method:

Usage syntax : lst.sort() #List object method call, lst is a list object, such as: lst=[1,24,5,2,3], which is exclusive to the list object

Method purpose : sorting the original list means changing the original list, see the figure below for details, the return value is None

Understanding explanation : the list object has a sort() method, which realizes sorting the original list and then returns, because python returns None by default, so lst.sort() must be pointed to a variable, then the value of this variable is None

② sorted function

Usage syntax : sorted(lst, args) #function parameter call, where lst is the object to be sorted: it can be a string or a dictionary, args is a sort according to the specified parameters, such as reverse, True means: reverse order Arrangement, False means: Arrange in positive order, there are other parameters that can be set

Function purpose : Sort the incoming parameter objects and return the sorted list, which does not apply to the original list

Understanding explanation : sorted() is a function, that is, process-oriented, which encapsulates the sorting process and processes it according to different objects. Different objects finally return data in the form of a list, that is, return a_lst,

(2) reverse method and reversed function: reverse sequence use

①The reverse method of the list

Usage syntax : lst.reverse() #object method call, lst is a list object, such as: lst=[1,24,5,2,3], which is exclusive to list objects

Method purpose : reverse the original list, change the original list, return value is None

Understanding explanation : The implementation is basically the same as the sort() method. The difference in function is to reverse a list and then return. Also because python returns None by default, so when you have to point lst.reverse() to a variable, then this The variable value is also None

②reversed function

Usage syntax : reversed(lst) #function parameter call, where lst is the object to be sorted: it needs to be a sequence, such as a list, a string

Method purpose : reverse the parameter object and return a reversed memory object instead of the reversed value

Understanding explanation : Different from the sorted function, the reversed function does not return a reversed list or string, but returns a memory object, and does not support the reversal of the dictionary

Summarize:

At this point, it is basically clear. sort and reverse are methods of list objects, unique methods of list objects, and these two methods operate on the original list and return directly. The return value is None, while the sorted function It supports the sorting of strings, dictionaries, and list objects. Instead of sorting on the original list, it returns a new list. The reveresd function is special, returning a memory object instead of the reversed value

Guess you like

Origin blog.csdn.net/Jolting/article/details/88037270