並べ替えとは、一連の値を昇順または降順に並べ替えることを意味します。 Python で値を並べ替えるにはさまざまな方法があります。保存するデータに応じて、リスト、タプル、辞書などのさまざまなデータ構造を使用して、値のセットまたはグループを保存できます。そこで、この記事では、Python でデータを並べ替える方法と基準について説明します。
Sorted() メソッド
これは、あらゆる種類のオブジェクトを並べ替える Python の事前定義されたメソッドです。
構文:
sorted(iterable, key, reverse)>
このメソッドでは、3 つのパラメータを渡します。そのうち 2 つ(キーとリバース)はオプションで、最初のパラメータ、つまり iterable は任意の反復可能なオブジェクトにすることができます。このメソッドはソートされたリストを返しますが、元のデータ構造は変更しません。
例 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))> |
>
>
['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']>
例 2:
事前定義された関数をキーパラメータとして使用します。したがって、2番目のパラメータ、つまり 鍵 指定されたデータ構造を、次のような事前定義された関数によってソートするために使用されます。 のみ() またはユーザー定義関数。キーパラメータに渡された関数に基づいて、データ構造内の値を並べ替えます。
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>))> |
>
>出力
Sorting without key parameter: ['apple', 'ball', 'cat', 'dog'] Sorting with len as key parameter: ['cat', 'dog', 'ball', 'apple']>
例 3:
キーパラメータにユーザー定義関数を使用します。
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))> |
>
>
出力
キーパラメータを使用しない並べ替え: [('Amar', 89), ('Lasya', 32), ('Ramesh', 56), ('Reka', 54)]
by_name をキーパラメータとして並べ替え: [('Amar', 89), ('Lasya', 32), ('Ramesh', 56), ('Reka', 54)]
by_marks をキーパラメータとして並べ替え: [('Lasya', 32), ('Reka', 54), ('Ramesh', 56), ('Amar', 89)]
例 4:
それで、 3番目のパラメータは逆です これは、反復可能オブジェクトを降順または降順にソートするために使用されます。
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>))> |
>
>出力
Sorting without key parameter: ['for', 'geeks', 'geeks'] Sorting with len as key parameter: ['geeks', 'geeks', 'for']>
例 5:
3 つのパラメータをすべて使用する
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>))> |
>
>
出力
キーと逆を使用しない並べ替え: [('Amar', 89), ('Lasya', 32), ('Ramesh', 56), ('Reka', 54)]
by_name をキー パラメーター、reverse パラメーターを False として並べ替えます: [('Amar', 89), ('Lasya', 32), ('Ramesh', 56), ('Reka', 54)]
by_name をキー パラメーター、reverse パラメーターを True として並べ替え: [('Reka', 54), ('Ramesh', 56), ('Lasya', 32), ('Amar', 89)]
by_marks をキー パラメーター、逆引きパラメーターを False として並べ替え: [('Lasya', 32), ('Reka', 54), ('Ramesh', 56), ('Amar', 89)]
by_marks をキー パラメーター、reverse パラメーターを True として並べ替え: [('Amar', 89), ('Ramesh', 56), ('Reka', 54), ('Lasya', 32)]
Sort() メソッド
このメソッドはデフォルトでリストを昇順に並べ替えますが、reverse パラメーターを使用して降順に並べ替えることができます。このメソッドは元のリストを変更しますが、何も返しません。
string.javaを含む
例 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)> |
>
>出力
Original list: ['geeks', 'for', 'geeks'] Sorted list: ['for', 'geeks', 'geeks']>
例 2:
事前定義された関数をキーパラメータとして使用する
Python3
# 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)> |
>
>出力
Original List: ['apple', 'ball', 'cat', 'dog'] Sorting with len as key parameter: ['cat', 'dog', 'ball', 'apple']>
例 3:
ユーザー定義関数をキーパラメータとして使用する
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)> |
>
>
出力
元のリスト: [('Ramesh', 56), ('Reka', 54), ('Lasya', 32), ('Amar', 89)]
by_name をキーパラメータとして並べ替え: [('Amar', 89), ('Lasya', 32), ('Ramesh', 56), ('Reka', 54)]
元のリスト: [('Ramesh', 56), ('Reka', 54), ('Lasya', 32), ('Amar', 89)]
by_marks をキーパラメータとして並べ替え: [('Lasya', 32), ('Reka', 54), ('Ramesh', 56), ('Amar', 89)]
例 4:
逆引きパラメータの使用
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)> |
>
>出力
original list: ['geeks', 'for', 'geeks'] sorting with reverse parameter ['geeks', 'geeks', 'for']>