„Python“ rūšiavimo sąrašo masyvo metodas - didėjantis ir mažėjantis, paaiškintas pavyzdžiais

Jei norite sužinoti, kaip dirbti su „ sort()Python“ projektų metodu, šis straipsnis skirtas jums. Šis metodas yra labai galingas ir jį galite pritaikyti pagal savo poreikius, todėl pažiūrėkime, kaip jis veikia išsamiai.

Tu išmoksi:

  • Kaip naudoti šį metodą ir pritaikyti jo funkcionalumą.
  • Kada jį naudoti, o kada nenaudoti.
  • Kaip tai pavadinti skirtingų argumentų derinių perdavimu.
  • Kaip rūšiuoti sąrašą didėjimo ir mažėjimo tvarka.
  • Kaip palyginti sąrašo elementus pagal tarpines vertes.
  • Kaip galite perduoti lambda funkcijas šiam metodui.
  • Kaip šis metodas lyginamas su sorted()funkcija.
  • Kodėl sort()metodas atlieka stabilią rūšiavimą.
  • Kaip mutacijos procesas vyksta užkulisiuose.

Ar tu pasiruošęs? Pradėkime! ⭐

? Paskirtis ir naudojimo atvejai

Naudodami sort()metodą, galite rūšiuoti sąrašą:

  • Didėjančia tvarka
  • Mažėjančia tvarka

Šis metodas yra naudojamas rūšiuoti vietoje sąrašą, kuris reiškia, kad jis mutuoja , tai ar keičiamos jos tiesiogiai nesukeldamas jokių papildomų kopijų, todėl atminkite:

Šiame straipsnyje sužinosite daugiau apie mutaciją (pažadu!), Tačiau dabar labai svarbu žinoti, kad sort()metodas modifikuoja sąrašą, todėl jo pradinė versija yra prarasta.

Todėl šį metodą turėtumėte naudoti tik tuo atveju, jei:

  • Norite visam laikui modifikuoti (rūšiuoti) sąrašą.
  • Jums nereikia saugoti originalios sąrašo versijos.

Jei tai atitinka jūsų poreikius, .sort()metodas yra būtent tai, ko ieškote.

? Sintaksė ir argumentai

Pažiūrėkime, kaip galite paskambinti, .sort()kad išnaudotumėte visas jo galimybes.

Tai yra paprasčiausias skambutis (be argumentų):

Jei nepateiksite jokių argumentų, pagal numatytuosius nustatymus:

  • Sąrašas bus rūšiuojamas didėjimo tvarka.
  • Sąrašo elementai bus lyginami tiesiogiai, naudojant jų vertes su <operatoriumi.

Pavyzdžiui:

>>> b = [6, 3, 8, 2, 7, 3, 9] >>> b.sort() >>> b [2, 3, 3, 6, 7, 8, 9] # Sorted!

Pasirinktiniai argumentai  

Norėdami pritaikyti sort()metodo veikimą, galite perduoti du pasirenkamus argumentus:

  • Raktas
  • Reversas

Pažiūrėkime, kaip jie keičia šio metodo elgesį. Čia mes turime metodo iškvietimą su šiais dviem argumentais:

Prieš paaiškindamas, kaip jie veikia, norėčiau paaiškinti tai, ką tikriausiai pastebėjote aukščiau pateiktoje diagramoje - metodo iškvietime parametrų pavadinimai turi būti įtraukti prieš jų atitinkamas reikšmes, pavyzdžiui:

  • key=
  • reverse=

Taip yra todėl, kad jie yra tik raktinių žodžių argumentai . Jei perduodate jiems pasirinktinę vertę, metodo iškvietime turi būti nurodyti jų pavadinimai , po kurių nurodomas lygybės ženklas =ir atitinkamos jų vertės, pavyzdžiui:

Kitu atveju, jei bandysite perduoti argumentus tiesiogiai, kaip paprastai darome padėties parametrams, pamatysite šią klaidą, nes funkcija nežinos, kuris argumentas atitinka kurį parametrą:

TypeError: sort() takes no positional arguments

Reversas

Dabar, kai žinote, kokie yra tik raktinių žodžių argumentai, pradėkime nuo reverse.

Vertė reversegali būti Truearba False:

  • False reiškia, kad sąrašas bus rūšiuojamas didėjimo tvarka.
  • True reiškia, kad sąrašas bus rūšiuojamas mažėjančia (atvirkštine) tvarka.

? Patarimas: Pagal numatytuosius nustatymus jo vertė yra False- jei nepateiksite jokių argumentų šiam parametrui, sąrašas rūšiuojamas didėjimo tvarka.

Čia mes turime keletą pavyzdžių:

# List of Integers >>> b = [6, 3, 8, 2, 7, 3, 9] >>> b.sort() >>> b [2, 3, 3, 6, 7, 8, 9] # List of Strings >>> c = ["A", "Z", "D", "T", "U"] >>> c.sort() >>> c ['A', 'D', 'T', 'U', 'Z'] 

? Patarimas: Jei sąrašo elementai yra eilutės, jie rūšiuojami abėcėlės tvarka.

# List of Integers >>> b = [6, 3, 8, 2, 7, 3, 9] >>> b.sort(reverse=True) >>> b [9, 8, 7, 6, 3, 3, 2] # List of Strings >>> c = ["A", "Z", "D", "T", "U"] >>> c.sort(reverse=True) >>> c ['Z', 'U', 'T', 'D', 'A']

? Patarimas: atkreipkite dėmesį, kaip sąrašas rūšiuojamas mažėjančia tvarka, jei reversetaip True.

Raktas

Dabar, kai žinote, kaip dirbti su reverseparametru, pažiūrėkime keyparametrą.

Šis parametras yra šiek tiek išsamesnis, nes jis nustato, kaip sąrašo elementai lyginami rūšiavimo proceso metu.

Vertė keyyra arba:

  • None, which means that the elements of the list will be compared directly. For example, in a list of integers, the integers themselves can be used for the comparison.
  • Afunction of one argument that generates an intermediate value for each element. This intermediate value is calculated only once and it's used to make the comparisons during the entire sorting process. We use this when we don't want to compare the elements directly, for example, when we want to compare strings based on their length (the intermediate value).

? Tip: By default, the value of key is None, so the elements are compared directly.

For example:

Let's say that we want to sort a list of strings based on their length, from the shortest string to the longest string. We can pass the function len as the value of key, like this:

>>> d = ["aaa", "bb", "c"] >>> d.sort(key=len) >>> d ['c', 'bb', 'aaa']

? Tip: Notice that we are only passing the name of the function (len) without parenthesis because we are not calling the function. This is very important.

Notice the difference between comparing the elements directly and comparing their length (see below). Using the default value of key (None) would have sorted the strings alphabetically (left), but now we are sorting them based on their length (right):

What happens behind the scenes? Each element is passed as an argument to the len() function, and the value returned by this function call is used to perform the comparisons during the sorting process:

This results in a list with a different sorting criteria: length.

Here we have another example:

Another interesting example is sorting a list of strings as if they were all written in lowercase letters (for example, making "Aa" equivalent to "aa").

According to lexicographical order, capital letters come before lowercase letters:

>>> "E" < "e" True

So the string "Emma" would come before "emily" in a sorted list, even if their lowercase versions would be in the opposite order:

>>> "Emma" >> "emma" < "emily" False

To avoid distinguishing between capital and lowercase letters, we can pass the function str.lower as key. This will generate a lowercase version of the strings that will be used for the comparisons:

>>> e = ["Emma", "emily", "Amy", "Jason"] >>> e.sort(key=str.lower) >>> e ['Amy', 'emily', 'Emma', 'Jason']

Notice that now, "emily" comes before "Emma" in the sorted list, which is exactly what we wanted.

? Tip: if we had used the default sorting process, all the strings that started with an uppercase letter would have come before all the strings that started with a lowercase letter:

>>> e = ["Emma", "emily", "Amy", "Jason"] >>> e.sort() >>> e ['Amy', 'Emma', 'Jason', 'emily']

Here is an example using Object-Oriented Programming (OOP):

If we have this very simple Python class:

>>> class Client: def __init__(self, age): self.age = age

And we create four instances:

>>> client1 = Client(67) >>> client2 = Client(23) >>> client3 = Client(13) >>> client4 = Client(35)

We can make a list that references them:

>>> clients = [client1, client2, client3, client4]

Then, if we define a function to get the age of these instances:

>>> def get_age(client): return client.age

We can sort the list based on their age by passing the get_age function an an argument:

>>> clients.sort(key=get_age)

This is the final, sorted version of the list. We use a for loop to print the age of the instances in the order that they appear in the list:

>>> for client in clients: print(client.age) 13 23 35 67

Exactly what we wanted – now the list is sorted in ascending order based on the age of the instances.

? Tip: Instead of defining a get_age function, we could have used a lambda function to get the age of each instance, like this:

>>> clients.sort(key=lambda x: x.age)

Lambda functions are small and simple anonymous functions, which means that they don't have a name. They are very helpful for these scenarios when we only want to use them in particular places for a very short period of time.

This is the basic structure of the lambda function that we are using to sort the list:

Passing Both Arguments

Awesome! Now you know to customize the functionality of the sort() method. But you can take your skills to a whole new level by combining the effect of key and reverse in the same method call:

>>> f = ["A", "a", "B", "b", "C", "c"] >>> f.sort(key=str.lower, reverse=True) >>> f ['C', 'c', 'B', 'b', 'A', 'a']

These are the different combinations of the arguments and their effect:

The Order of Keyword-Only Arguments Doesn't Matter

Since we are specifying the names of the arguments, we already know which value corresponds to which parameter, so we can include either key or reverse first in the list and the effect will be exactly the same.

So this method call:

Is equivalent to:

This is an example:

>>> a = ["Zz", "c", "y", "o", "F"] >>> a.sort(key=str.lower, reverse=True) >>> a ['Zz', 'y', 'o', 'F', 'c']

If we change the order of the arguments, we get the exact same result:

>>> a = ["Zz", "c", "y", "o", "F"] >>> a.sort(reverse=True, key=str.lower) >>> a ['Zz', 'y', 'o', 'F', 'c']

? Return Value

Dabar pakalbėkime šiek tiek apie šio metodo grąžinimo vertę. Į sort()metodas grįžta None- tai ne grąžinti surūšiuoti versija sąrašą, kaip mes galime intuityviai tikėtis.

Pagal „Python“ dokumentaciją:

Norėdamas priminti vartotojams, kad jis veikia kaip šalutinis poveikis, jis negrąžina rūšiuojamos sekos.

Iš esmės tai naudojama mums priminti, kad mes keičiame pirminį sąrašą atmintyje, o ne generuojame naują sąrašo kopiją.

Tai yra grąžinamosios vertės pavyzdys sort():

>>> nums = [6.5, 2.4, 7.3, 3.5, 2.6, 7.4] # Assign the return value to this variable: >>> val = nums.sort() # Check the return value: >>> print(val) None

Pamatyti? Nonebuvo grąžintas metodo skambučiu.

? Tip: It is very important not to confuse the sort() method with the sorted() function, which is a function that works very similarly, but doesn't modify the original list. Instead sorted() generates and returns a new copy of the list, already sorted.

This is an example that we can use to compare them:

# The sort() method returns None >>> nums = [6.5, 2.4, 7.3, 3.5, 2.6, 7.4] >>> val = nums.sort() >>> print(val) None
# sorted() returns a new sorted copy of the original list >>> nums = [6.5, 2.4, 7.3, 3.5, 2.6, 7.4] >>> val = sorted(nums) >>> val [2.4, 2.6, 3.5, 6.5, 7.3, 7.4] # But it doesn't modify the original list >>> nums [6.5, 2.4, 7.3, 3.5, 2.6, 7.4]

This is very important because their effect is very different. Using the sort() method when you intended to use sorted() can introduce serious bugs into your program because you might not realize that the list is being mutated.

? The sort() Method Performs a Stable Sort

Now let's talk a little bit about the characteristics of the sorting algorithm used by sort().

Šis metodas atlieka stabilų rūšiavimą, nes jis veikia įgyvendinant „TimSort“ - labai efektyvų ir stabilų rūšiavimo algoritmą.

Pagal „Python“ dokumentaciją:

Rūšiavimas yra stabilus, jei jis garantuoja , kad nekeičiama santykinė elementų, kurie lygūs vienodai, tvarka - tai naudinga rūšiuojant kelis kartus (pvz., Rūšiuoti pagal skyrių, tada pagal atlyginimo lygį).

Tai reiškia, kad jei du elementai turi tą pačią arba tarpinę vertę (raktą), garantuojama, kad jie išliks ta pačia tvarka vienas kito atžvilgiu.

Pažiūrėkime, ką noriu pasakyti. Kelias akimirkas pažiūrėkite į šį pavyzdį:

>>> d = ["BB", "AA", "CC", "A", "B", "AAA", "BBB"] >>> d.sort(key=len) >>> d ['A', 'B', 'BB', 'AA', 'CC', 'AAA', 'BBB']

Mes lyginame elementus pagal jų ilgį, nes mes perdavėme lenfunkciją kaip argumentą key.

We can see that there are three elements with length 2: "BB", "AA", and "CC" in that order.

Now, notice that these three elements are in the same relative order in the final sorted list:

This is because the algorithm is guaranteed to be stable and the three of them had the same intermediate value (key) during the sorting process (their length was 2, so their key was 2).

? Tip: The same happened with "A" and "B" (length 1) and "AAA" and "BBB" (length 3), their original order relative to each other was preserved.

Now you know how the sort() method works, so let's dive into mutation and how it can affect your program.

? Mutation and Risks

As promised, let's see how the process of mutation works behind the scenes:

When you define a list in Python, like this:

a = [1, 2, 3, 4]

You create an object at a specific memory location. This location is called the "memory address" of the object, represented by a unique integer called an id.

You can think of an id as a "tag" used to identify a specific place in memory:

You can access a list's id using the id() function, passing the list as argument:

>>> a = [1, 2, 3, 4] >>> id(a) 60501512

When you mutate the list, you change it directly in memory. You may ask, why is this so risky?

It's risky because it affects every single line of code that uses the list after the mutation, so you may be writing code to work with a list that is completely different from the actual list that exists in memory after the mutation.

This is why you need to be very careful with methods that cause mutation.

In particular, the sort() method mutates the list. This is an example of its effect:

Here is an example:

# Define a list >>> a = [7, 3, 5, 1] # Check its id >>> id(a) 67091624 # Sort the list using .sort() >>> a.sort() # Check its id (it's the same, so the list is the same object in memory) >>> id(a) 67091624 # Now the list is sorted. It has been mutated! >>> a [1, 3, 5, 7]

The list was mutated after calling .sort().

Every single line of code that works with list a after the mutation has occurred will use the new, sorted version of the list. If this was not what you intended, you may not realize that other parts of your program are working with the new version of the list.

Here is another example of the risks of mutation within a function:

# List >>> a = [7, 3, 5, 1] # Function that prints the elements of the list in ascending order. >>> def print_sorted(x): x.sort() for elem in x: print(elem) # Call the function passing 'a' as argument >>> print_sorted(a) 1 3 5 7 # Oops! The original list was mutated. >>> a [1, 3, 5, 7]

The list a that was passed as argument was mutated, even if that wasn't what you intended when you initially wrote the function.

? Tip: If a function mutates an argument, it should be clearly stated to avoid introducing bugs into other parts of your program.

? Summary of the sort() Method

  • The sort() method lets you sort a list in ascending or descending order.
  • It takes two keyword-only arguments: key and reverse.
  • reverse determines if the list is sorted in ascending or descending order.
  • key is a function that generates an intermediate value for each element, and this value is used to do the comparisons during the sorting process.
  • The sort() method mutates the list, causing permanent changes. You need to be very careful and only use it if you do not need the original version of the list.

Labai tikiuosi, kad jums patiko mano straipsnis ir jis buvo naudingas. Dabar galite naudotisort()metodą savo „Python“ projektuose. Peržiūrėkite mano internetinius kursus. Sekite mane „Twitter“. ⭐️