logo

Jak seřadit sadu hodnot v Pythonu?

Třídění znamená uspořádání množiny hodnot buď rostoucím nebo klesajícím způsobem. Existují různé metody řazení hodnot v Pythonu. Můžeme uložit množinu nebo skupinu hodnot pomocí různých datových struktur, jako je seznam, n-tice, slovníky, což závisí na datech, která ukládáme. V tomto článku tedy probereme některé metody a kritéria pro třídění dat v Pythonu.

Metoda Sorted().

Toto je předdefinovaná metoda v pythonu, která třídí jakýkoli druh objektu.



Syntax:

sorted(iterable, key, reverse)>

V této metodě předáváme 3 parametry, z nichž 2 (klíčový a reverzní) jsou volitelné a první parametr, tj. iterovatelný může být libovolný iterovatelný objekt. Tato metoda vrací seřazený seznam, ale nemění původní datovou strukturu.

Příklad 1:



Python3






# List> list_of_items>=> [>'g'>,>'e'>,>'e'>,>'k'>,>'s'>]> print>(>sorted>(list_of_items))> # Tuple> tuple_of_items>=> (>'g'>,>'e'>,>'e'>,>'k'>,>'s'>)> print>(>sorted>(tuple_of_items))> # String-sorted based on ASCII> # translations> string>=> 'geeks'> print>(>sorted>(string))> # Dictionary> dictionary>=> {>'g'>:>1>,>'e'>:>2>,>'k'>:>3>,>'s'>:>4>}> print>(>sorted>(dictionary))> # Set> set_of_values>=> {>'g'>,>'e'>,>'e'>,>'k'>,>'s'>}> print>(>sorted>(set_of_values))> # Frozen Set> frozen_set>=> frozenset>((>'g'>,>'e'>,>'e'>,>'k'>,>'s'>))> print>(>sorted>(frozen_set))>

>

>


Výstup

['e', 'e', 'g', 'k', 's'] ['e', 'e', 'g', 'k', 's'] ['e', 'e', 'g', 'k', 's'] ['e', 'g', 'k', 's'] ['e', 'g', 'k', 's'] ['e', 'g', 'k', 's']>

Příklad 2:

Použití předdefinované funkce jako klíčového parametru. Takže druhý parametr, tj. klíč slouží k řazení dané datové struktury podle nějaké předdefinované funkce jako je např jen() nebo nějaká uživatelsky definovaná funkce. Seřadí hodnoty v datové struktuře na základě funkce předané parametru key.

Python3




# using key parameter with pre-defined> # function i.e. len()> list_of_items>=> [>'apple'>,>'ball'>,>'cat'>,>'dog'>]> print>(>'Sorting without key parameter:'>,>sorted>(list_of_items))> print>(>'Sorting with len as key parameter:'>,>sorted>(list_of_items, key>=>len>))>

>

>

Výstup

Sorting without key parameter: ['apple', 'ball', 'cat', 'dog'] Sorting with len as key parameter: ['cat', 'dog', 'ball', 'apple']>

Příklad 3:

Použití uživatelem definované funkce pro klíčový parametr.

Python3




# using key parameter with user-defined> # function i.e. by_name> # using key parameter with user-defined> # function i.e. by_marks> # here is a list_of_tuples where the first> # item in tuple is the student name and the> # second item is his/her marks> list_of_items>=> [(>'Ramesh'>,>56>),(>'Reka'>,>54>),(>'Lasya'>,>32>),(>'Amar'>,>89>)]> # defining a user-defined function which returns> # the first item(name)> def> by_name(ele):> >return> ele[>0>]> # defining a user-defined function which returns> # the second item(marks)> def> by_marks(ele):> >return> ele[>1>]> print>(>'Sorting without key parameter:'>,>sorted>(list_of_items))> print>(>'Sorting with by_name as key parameter:'>,> >sorted>(list_of_items, key>=>by_name))> print>(>'Sorting with by_marks as key parameter:'>,> >sorted>(list_of_items, key>=>by_marks))>

>

>

Výstup

Řazení bez klíčového parametru: [('Amar', 89), ('Lasya', 32), ('Ramesh', 56), ('Reka', 54)]

Řazení s klíčovým parametrem podle_name: [(‘Amar‘, 89), (‘Lasya‘, 32), (‘Ramesh‘, 56), (‘Reka‘, 54)]

Řazení s klíčovým parametrem by_marks: [(‘Lasya‘, 32), (‘Reka‘, 54), (‘Ramesh‘, 56), (‘Amar‘, 89)]

Příklad 4:

Takže 3. parametr je obrácený který se používá k řazení iterovatelných v sestupném nebo sestupném pořadí.

Python3




# using key parameter reverse> list_of_items>=> [>'geeks'>,>'for'>,>'geeks'>]> print>(>'Sorting without key parameter:'>,> >sorted>(list_of_items))> print>(>'Sorting with len as key parameter:'>,> >sorted>(list_of_items, reverse>=>True>))>

>

>

Výstup

Sorting without key parameter: ['for', 'geeks', 'geeks'] Sorting with len as key parameter: ['geeks', 'geeks', 'for']>

Příklad 5:

smyčka bash while

Pomocí všech tří parametrů

Python3




# using by_name and by_marks as key parameter> # and making reverse parameter true> # here is a list_of_tuples where the first> # item in tuple is the student name and the> # second item is his/her marks> list_of_items>=> [(>'Ramesh'>,>56>), (>'Reka'>,>54>),> >(>'Lasya'>,>32>), (>'Amar'>,>89>)]> # defining a user-defined function which> # returns the first item(name)> def> by_name(ele):> >return> ele[>0>]> # defining a user-defined function which> # returns the second item(marks)> def> by_marks(ele):> >return> ele[>1>]> print>(>'Sorting without key and reverse:'>,>sorted>(list_of_items))> print>(>'Sorting with by_name as key parameter and reverse parameter as False:'>,> >sorted>(list_of_items, key>=>by_name, reverse>=>False>))> print>(>'Sorting with by_name as key parameter and reverse parameter as True:'>,> >sorted>(list_of_items, key>=>by_name, reverse>=>True>))> print>(>'Sorting with by_marks as key parameter and reverse parameter as False:'>,> >sorted>(list_of_items, key>=>by_marks, reverse>=>False>))> print>(>'Sorting with by_marks as key parameter and reverse parameter as True:'>,> >sorted>(list_of_items, key>=>by_marks, reverse>=>True>))>

>

>

Výstup

Řazení bez klíče a zpětného chodu: [(‘Amar‘, 89), (‘Lasya‘, 32), (‘Ramesh‘, 56), (‘Reka‘, 54)]

Řazení s klíčovým parametrem podle_name a zpětným parametrem jako False: [(‘Amar‘, 89), (‘Lasya‘, 32), (‘Ramesh‘, 56), (‘Reka‘, 54)]

Řazení s klíčovým parametrem by_name a zpětným parametrem jako True: [(‘Reka‘, 54), (‘Ramesh‘, 56), (‘Lasya‘, 32), (‘Amar‘, 89)]

Řazení pomocí by_marks jako klíčového parametru a zpětného parametru jako False: [(‘Lasya‘, 32), (‘Reka‘, 54), (‘Ramesh‘, 56), (‘Amar‘, 89)]

Řazení s klíčovým parametrem by_marks a zpětným parametrem jako True: [(‘Amar‘, 89), (‘Ramesh‘, 56), (‘Reka‘, 54), (‘Lasya‘, 32)]

Metoda řazení ().

Tato metoda standardně třídí seznam ve vzestupném pořadí a pro řazení sestupně můžeme použít parametr reverse. Tato metoda změní původní seznam a nic nevrací.

Příklad 1:

Python3




# creating a list of items> list_of_items>=> [>'geeks'>,>'for'>,>'geeks'>]> print>(>'Original list:'>, list_of_items)> # using the sort method to sort> # the items> list_of_items.sort()> # displaying the list> print>(>'Sorted list:'>, list_of_items)>

>

>

Výstup

Original list: ['geeks', 'for', 'geeks'] Sorted list: ['for', 'geeks', 'geeks']>

Příklad 2:

Použití předdefinované funkce jako klíčového parametru

Python3




přejmenování adresáře v linuxu
# using key parameter with pre-defined> # function i.e. len()> list_of_items>=> [>'apple'>,>'ball'>,>'cat'>,>'dog'>]> print>(>'Original List:'>, list_of_items)> # using the len() as key parameter and> # sorting the list> list_of_items.sort(key>=>len>)> print>(>'Sorting with len as key parameter:'>, list_of_items)>

>

>

Výstup

Original List: ['apple', 'ball', 'cat', 'dog'] Sorting with len as key parameter: ['cat', 'dog', 'ball', 'apple']>

Příklad 3:

Použití uživatelem definované funkce jako klíčového parametru

Python3




# using key parameter with user-defined> # function i.e. by_name> # using key parameter with user-defined> # function i.e. by_marks> # defining a user-defined function which> # returns the first item(name)> def> by_name(ele):> >return> ele[>0>]> # defining a user-defined function which> # returns the second item(marks)> def> by_marks(ele):> >return> ele[>1>]> # here is a list_of_tuples where the first> # item in tuple is the student name and the> # second item is his/her marks> list_of_items>=> [(>'Ramesh'>,>56>), (>'Reka'>,>54>),> >(>'Lasya'>,>32>), (>'Amar'>,>89>)]> print>(>'original list:'>, list_of_items)> # sorting by key value as by_name function> list_of_items.sort(key>=>by_name)> print>(>'Sorting with by_name as key parameter:'>, list_of_items)> # here is a list_of_tuples where the first> # item in tuple is the student name and the> # second item is his/her marks> list_of_items>=> [(>'Ramesh'>,>56>), (>'Reka'>,>54>),> >(>'Lasya'>,>32>), (>'Amar'>,>89>)]> print>(>'original list:'>, list_of_items)> # sorting by key value as by_marks function> list_of_items.sort(key>=>by_marks)> print>(>'Sorting with by_marks as key parameter:'>, list_of_items)>

>

>

Výstup

původní seznam: [(‘Ramesh‘, 56), (‘Reka‘, 54), (‘Lasya‘, 32), (‘Amar‘, 89)]

Řazení s klíčovým parametrem podle_name: [(‘Amar‘, 89), (‘Lasya‘, 32), (‘Ramesh‘, 56), (‘Reka‘, 54)]

původní seznam: [(‘Ramesh‘, 56), (‘Reka‘, 54), (‘Lasya‘, 32), (‘Amar‘, 89)]

Řazení s klíčovým parametrem by_marks: [(‘Lasya‘, 32), (‘Reka‘, 54), (‘Ramesh‘, 56), (‘Amar‘, 89)]

Příklad 4:

Použití reverzního parametru

Python3




# using key parameter reverse> list_of_items>=> [>'geeks'>,>'for'>,>'geeks'>]> print>(>'original list:'>, list_of_items)> list_of_items.sort(reverse>=>True>)> print>(>'sorting with reverse parameter'>, list_of_items)>

>

>

Výstup

original list: ['geeks', 'for', 'geeks'] sorting with reverse parameter ['geeks', 'geeks', 'for']>