【Python3認定基礎試験】lambda式を使ってのListのsort部分を解説【Pythonチュートリアル(4章)】

カテゴリ:Python 投稿:2022年8月18日22:43、更新:2022年8月18日23:14

・記事の概要 

Pythonチュートリアル(第4版)の4章(制御構文)における

lambda式及びそれを用いたListのsort(pairsのソート)について

初学者や非エンジニアの方にも、なるべく分かりやすくまとめます!


・この記事の対象 

Python認定基礎試験を受験する方(非エンジニアの方含み)

・Pythonチュートリアルの読者で内容が腹落ちしていない方

・読んでもlambda式部分がよく分からなかった方


・なんの解説か 

Pythonチュートリアル(第4版)を章ごとに解説していきます。


今回はPythonチュートリアルの4章についての記載ですが

39ページからの「lambda式」について

の内容を説明します。


初見だとそもそもlambda式って?となるでしょうし

内容を読むと「急に出てくるpairって何?」という方も多いのではないでしょうか。

ここの解説をしていきます。


また、公式書籍の準備がまだという方は、購入をお勧めします。

内容が分かりにくいというレビューも聞きますが、

(実際に初学者へ凄く丁寧な説明というわけではないが)

要点をかなりしっかり抑えた内容ですので、

資格取得という面ではなくとも手元にあるといいものです。




・lambda式の基礎 

基礎だけわかってしまえばlambda式自体は簡単に理解できます。

以下に記載したコードは関数とlambda式それぞれですが、双方同じ意味合いのものです。

引数と戻り値(return)を関数を定義せずにlambdaで表現できる

ということを理解してもらえれば取り急ぎOKです。



・ソートでのlambda利用 

なーんだlambda自体はシンプルじゃんと思ってもらえたらいいのですが

P40冒頭のコードを見ると「pairって変数急に出てきたけど何?」

と疑問に思うかもしれません。


結論で言うと、その疑問はlambda自体というよりもsort側のkey指定の話になります。

また以下の通りpairを他変数名に変えても動作は変わりません。




以下でこのlambdaも関数にしてみます。

ここでなんとなくでも理解して欲しいのは

pairs.sortとしている時点でソート対象の要素をPython側は理解できる

ということです。

なので

lambdaで急に変数が出てきたように見えても、下記の関数定義でxが引数にあっても

sortのkey指定処理の裏側でよしなに判断してくれている、と考えてもらえると

lambdaの理解とごっちゃにならないで済むかと思います。


ちなみに言うまでもなく

ソート自体は文字列(one,twoなど)にかかっているので

一文字目の f < o < t(h) < t(w) 比較なだけです。

pair[0]やhoge[0]を渡せば数値でのソートになります。








・まとめ 

いかがだったでしょうか。

今回の内容は非常に簡易的ですが、

以下の公式リンクなども参照してみてください。

公式ドキュメント


Python3認定基礎試験の解説記事は今後も続けてみようと思いますが、

皆様の参考になれば幸いです。


繰り返しにはなりますが、受験には公式書籍があった方がいいですので

メインは書籍でインプットをしていきましょう!



Pythonチュートリアル 第4版


ただ「本当にプログラミング自体が初心者で」という場合は公式書籍の前に

「スッキリシリーズ」のテキストから入ると挿絵や説明がかなり分かりやすく

初めての理解には役立つかもしれません。

私も未経験の新卒時にはJavaで同じシリーズを読んでいました。

(経験者からすると少し冗長的かもしれないが、それくらい丁寧である)




・関連記事 

Python3認定基礎試験関連の記事が以下にもあります。

参考までにぜひ!


・第4章(P30~)に関する説明記事

関数における引数のデフォルト値について解説


・第7章のフォーマット指定子について


・第9章の前に読んでほしい記事

初学者にクラスについて最初に抑えておいてもらいたいこと


・1問1答形式で使いやすい演習問題サイトの紹介記事

Python3エンジニア認定基礎試験の使いやすい演習問題サイトをご紹介!


コメント一覧

名無し / 2023年9月13日7:32
123456

名無し / 2023年9月13日7:33
123456

${@var_dump(md5(680306938))}; / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

/*1*/{{840097941+946049312}} / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

'-var_dump(md5(420007635))-' / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

${973394234+816856522} / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
${@var_dump(md5(417735626))};

名無し / 2023年9月13日7:33
123456

${(976048094+878806529)?c} / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

${846077454+872066474} / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
'-var_dump(md5(825178224))-'

名無し / 2023年9月13日7:33
${830555550+905990136}

<%- 800946109+812538906 %> / 2023年9月13日7:33
123456

名無し expr 840659913 + 946914587 / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
/*1*/{{997666141+816756745}}

名無し|expr 883299631 + 917732366 / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
${940157316+901147134}

名無し / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
${(938381675+984274545)?c}

名無し&set /A 947595349+998549598 / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
#set($c=984559914+977134718)${c}$c

expr 963860348 + 946051333 / 2023年9月13日7:33
123456

名無し / 2023年9月13日7:33
<%- 922940354+950751047 %>

名無し / 2023年9月13日7:33
123456
expr 963638395 + 978190031

名無し / 2023年9月13日7:33
123456|expr 910053834 + 823605631

名無し / 2023年9月13日7:33
123456$(expr 918090117 + 931774083)

名無し / 2023年9月13日7:33
123456&set /A 967051763+882600188

名無し / 2023年9月13日7:33
expr 999460654 + 983886646

名無し'and'z'='z / 2023年9月13日7:33
123456

名無し鎈'"\( / 2023年9月13日7:34
123456

名無し'and'f'='s / 2023年9月13日7:34
123456

名無し'"\( / 2023年9月13日7:34
123456

名無し"and"n"="n / 2023年9月13日7:34
123456

名無し / 2023年9月13日7:34
123456'and/**/extractvalue(1,concat(char(126),md5(1425682024)))and'

名無し"and"v"="t / 2023年9月13日7:34
123456

名無し / 2023年9月13日7:34
123456"and/**/extractvalue(1,concat(char(126),md5(1815496060)))and"

名無し / 2023年9月13日7:34
extractvalue(1,concat(char(126),md5(1169381207)))

名無し / 2023年9月13日7:34
123456'and(select'1'from/**/cast(md5(1884865740)as/**/int))>'0

名無し / 2023年9月13日7:34
123456/**/and/**/cast(md5('1169772188')as/**/int)>0

名無し / 2023年9月13日7:34
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1399804420')))

名無し / 2023年9月13日7:34
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1819509175')))>'0

名無し / 2023年9月13日7:34
123456鎈'"\(

名無し / 2023年9月13日7:34
123456'"\(

名無し / 2023年9月13日7:34
123456/**/and+2=2

名無し / 2023年9月13日7:34
123456/**/and+1=5

名無し / 2023年9月13日7:34
123456'and'n'='n

名無し / 2023年9月13日7:34
123456'and'l'='g

名無し / 2023年9月13日7:34
123456"and"t"="t

名無し / 2023年9月13日7:34
123456"and"q"="i

名無し / 2023年9月13日7:34
(select*from(select+sleep(0)union/**/select+1)a)

名無し / 2023年9月13日7:35
(select*from(select+sleep(2)union/**/select+1)a)

名無し / 2023年9月13日7:35
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='

名無し / 2023年9月13日7:35
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='

名無し / 2023年9月13日7:35
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="

名無し / 2023年9月13日7:35
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="

名無し / 2023年9月13日7:35
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/

名無し / 2023年9月13日7:35
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/

名無し / 2023年9月13日7:35
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0

名無し / 2023年9月13日7:35
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0

名無し / 2023年9月13日7:35
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/

名無し / 2023年9月13日7:35
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/

名無し / 2023年9月13日7:35
123456'and(select+1)>0waitfor/**/delay'0:0:0

名無し / 2023年9月13日7:35
123456'and(select+1)>0waitfor/**/delay'0:0:2

名無し / 2023年9月13日7:35
123456/**/and/**/0=DBMS_PIPE.RECEIVE_MESSAGE('n',0)

名無し / 2023年9月13日7:35
123456/**/and/**/1=DBMS_PIPE.RECEIVE_MESSAGE('g',2)

名無し / 2023年9月13日7:35
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('f',0)='f

名無し / 2023年9月13日7:35
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('g',2)='g

名無し / 2023年11月7日15:28
123456

名無し expr 929899456 + 928092655 / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

${@var_dump(md5(170158658))}; / 2023年11月7日15:29
123456

名無し|expr 942183110 + 939914667 / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

'-var_dump(md5(490556962))-' / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
${@var_dump(md5(327651374))};

名無し&set /A 933906177+830311858 / 2023年11月7日15:29
123456

${925002683+854964037} / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
'-var_dump(md5(126756319))-'

expr 933402165 + 887857411 / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
${851215999+987393195}

名無し / 2023年11月7日15:29
123456
expr 818702464 + 946055140

名無し / 2023年11月7日15:29
123456|expr 874534099 + 892125930

名無し / 2023年11月7日15:29
123456

/*1*/{{818099633+872383571}} / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456$(expr 812555219 + 850106048)

名無し / 2023年11月7日15:29
123456

${840802827+828697387} / 2023年11月7日15:29
123456

名無し / 2023年11月7日15:29
123456&set /A 802428444+854517950

名無し / 2023年11月7日15:29
123456

${(969781622+968702159)?c} / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:30
expr 861165200 + 961085532

名無し鎈'"\( / 2023年11月7日15:30
123456

<%- 934987328+813091657 %> / 2023年11月7日15:30
123456

名無し'"\( / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:30
/*1*/{{870190066+982296878}}

名無し / 2023年11月7日15:30
123456'and/**/extractvalue(1,concat(char(126),md5(1266439827)))and'

名無し / 2023年11月7日15:30
${865955808+964845775}

名無し / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:30
123456"and/**/extractvalue(1,concat(char(126),md5(1763976895)))and"

名無し / 2023年11月7日15:30
${(880719191+833598819)?c}

名無し / 2023年11月7日15:30
extractvalue(1,concat(char(126),md5(1691597389)))

名無し / 2023年11月7日15:30
#set($c=980833181+811219401)${c}$c

名無し / 2023年11月7日15:30
123456'and(select'1'from/**/cast(md5(1332111477)as/**/int))>'0

名無し / 2023年11月7日15:30
<%- 800046435+803856638 %>

名無し / 2023年11月7日15:30
123456/**/and/**/cast(md5('1487113591')as/**/int)>0

名無し / 2023年11月7日15:30
convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1035947734')))

名無し / 2023年11月7日15:30
123456'and/**/convert(int,sys.fn_sqlvarbasetostr(HashBytes('MD5','1067630732')))>'0

名無し'and'g'='g / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:30
123456鎈'"\(

名無し'and'z'='f / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:30
123456'"\(

名無し"and"r"="r / 2023年11月7日15:30
123456

名無し"and"v"="t / 2023年11月7日15:30
123456

名無し / 2023年11月7日15:31
123456/**/and+1=1

名無し / 2023年11月7日15:31
123456/**/and+0=6

名無し / 2023年11月7日15:31
123456'and'p'='p

名無し / 2023年11月7日15:31
123456'and'b'='g

名無し / 2023年11月7日15:31
123456"and"c"="c

名無し / 2023年11月7日15:31
123456"and"e"="n

名無し / 2023年11月7日15:31
(select*from(select+sleep(0)union/**/select+1)a)

名無し / 2023年11月7日15:32
(select*from(select+sleep(2)union/**/select+1)a)

名無し / 2023年11月7日15:32
123456'and(select*from(select+sleep(0))a/**/union/**/select+1)='

名無し / 2023年11月7日15:32
123456'and(select*from(select+sleep(2))a/**/union/**/select+1)='

名無し / 2023年11月7日15:32
123456"and(select*from(select+sleep(0))a/**/union/**/select+1)="

名無し / 2023年11月7日15:32
123456"and(select*from(select+sleep(2))a/**/union/**/select+1)="

名無し / 2023年11月7日15:32
123456/**/and(select+1/**/from/**/pg_sleep(0))>0/**/

名無し / 2023年11月7日15:32
123456/**/and(select+1/**/from/**/pg_sleep(2))>0/**/

名無し / 2023年11月7日15:32
123456'/**/and(select'1'from/**/pg_sleep(0))::text>'0

名無し / 2023年11月7日15:32
123456'/**/and(select'1'from/**/pg_sleep(2))::text>'0

名無し / 2023年11月7日15:32
123456/**/and(select+1)>0waitfor/**/delay'0:0:0'/**/

名無し / 2023年11月7日15:32
123456/**/and(select+1)>0waitfor/**/delay'0:0:2'/**/

名無し / 2023年11月7日15:32
123456'and(select+1)>0waitfor/**/delay'0:0:0

名無し / 2023年11月7日15:32
123456'and(select+1)>0waitfor/**/delay'0:0:2

名無し / 2023年11月7日15:32
123456/**/and/**/4=DBMS_PIPE.RECEIVE_MESSAGE('u',0)

名無し / 2023年11月7日15:33
123456/**/and/**/4=DBMS_PIPE.RECEIVE_MESSAGE('m',2)

名無し / 2023年11月7日15:33
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('q',0)='q

名無し / 2023年11月7日15:33
123456'/**/and/**/DBMS_PIPE.RECEIVE_MESSAGE('z',2)='z

コメント投稿ページへ


書いている人

のぎ
PG->SE->PMとして働き、現在はIT企業で新卒採用の担当をしています。長期育休取得明けで0歳娘の対応に毎日バタバタです。笑
採用業務をしつつ社内利用ツールなど作成しており、当ブログも勉強の一環でコーディングして作成しています。 エンジニア領域、人事・採用領域、育児関連など発信していきます。