LoginSignup
0
0

More than 3 years have passed since last update.

Python lambda,map,listの1行のコードを解読

Last updated at Posted at 2019-12-03

背景

python初心者です。
yolov3のコードを勉強しているとき、 lambda,map,listが1行で表記されていたので、頭が痛くなった。
簡単に自分のメモとしても残しておきたく、記載します。

lambda,map,listの1行のコード

keras-yolov3のyolo.pyにおける85行辺りのコードである。

1_yolo_code_around_85.py
self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))

困難にしている要素とそれのザックリ解説
・(*x):可変長引数
・lambda:無名関数   
・map:組み込み関数
・list:リスト化

hsv_to_rgbではhsvに対応する引数が3つ必要である。
そのため以下のようにも変更できる。

2_yolo_code_around_85.py
self.colors = list(map(lambda x: colorsys.hsv_to_rgb(x[0],x[1],x[2]), hsv_tuples))

リストxを上記のようにするとイメージがしやすいです。
lambdaによってリストxを用いた関数を定義し、
hsv_tuplesの値を関数に代入します。
mapにより、全要素に同じ処理を行い、
リスト化する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0