Lambda関数でソートする
2022/1/28 2022/4/15
PythonのLambda関数を使ってソートする方法です。
サンプルコード
city_code = [(10, "Kanagawa"), (20, "Tokyo"), (50, "Hokkaido"), (15, "Shizuoka"), (17, "Chiba")]
print(city_code)
pref = city_code
pref.sort(key = lambda pref: pref[0])
print(pref)
出力結果
>>>
*** Remote Interpreter Reinitialized ***
[(10, 'Kanagawa'), (20, 'Tokyo'), (50, 'Hokkaido'), (15, 'Shizuoka'), (17, 'Chiba')]
[(10, 'Kanagawa'), (15, 'Shizuoka'), (17, 'Chiba'), (20, 'Tokyo'), (50, 'Hokkaido')]