python3

python3 でタプルを指定した要素で順に並び替える

投稿日:2020年12月3日 更新日:

タプルを、指定した要素で並び替えることができる。

「key=lambda x 」で、引数 x を指定。lambda は無名関数を表す。

record = [('織田信長', 1534, 1582), ('豊臣秀頼', 1593, 1615), ('徳川家康', 1543, 1616)]

# 2番目の要素で並び替え
record.sort(key=lambda x: x[1], reverse=False) 
print(record)

# 3番目の要素で並び替え
record.sort(key=lambda x: x[2], reverse = False)
print(record)

結果

[('織田信長', 1534, 1582), ('徳川家康', 1543, 1616), ('豊臣秀頼', 1593, 1615)]
[('織田信長', 1534, 1582), ('豊臣秀頼', 1593, 1615), ('徳川家康', 1543, 1616)]

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib で、凡例を表示する方法

データに label を関連付け、plt.legend() で凡例を表示する。 例 from matplotlib import pyplot as plt data_x = [25,26,2 …

no image

matplotlib で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …

no image

matplotlib でグラフ表示ウィンドウの画面上の位置を自由に設定する方法

matplotlib.use(‘TkAgg’) としておき、 get_current_fig_manager().window.wm_geometry(“+20+50”) として、(+20+50)のと …

no image

python3 で、リストを pop したものの返り値

リストを pop すると、 pop された値が返される。 例 plant_1 = [’pumpkin’, ‘ginger’, ‘potato’] plant_2 = [] print( …

no image

matplotlib の ax でx軸、y軸の端の値を設定する

axes には、set_xlim として定義する。 例 import numpy as np from matplotlib import pyplot as plt x = np.linspace( …