Decrypt one line of code in Python lambda, map, list

background

I'm new to python. When I was studying the code for yolov3, I had a headache because lambda, map, and list were written on one line. I would like to write it as a memo for myself.

One line of code for lambda, map, list

It is the code around 85 lines in yolo.py of keras-yolov3.

1_yolo_code_around_85.py


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

Difficult elements and explanations of them ・ (* X): Variadic argument ・ Lamb: Anonymous function ・ Map: Built-in function ・ List: List

hsv_to_rgb requires 3 arguments corresponding to hsv. Therefore, it can be changed as follows.

2_yolo_code_around_85.py


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

It is easy to imagine if the list x is made as above. Define a function using list x by lambda Assign the value of hsv_tuples to the function. Use map to do the same for all elements List.

Recommended Posts

Decrypt one line of code in Python lambda, map, list
Fizzbuzz in Python (in one line)
List of Python code used in big data analysis
Make python segfault in one line
CGI server (1) python edition in one line
Display a list of alphabets in Python 3
Decompose command arguments in one line in Python
Summary of built-in methods in Python list
[Python] Invert bool value in one line
Get the EDINET code list in Python
[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
Make a rock-paper-scissors game in one line (python)
List of Python code to move and remember
Sorted list in Python
List of python modules
One liner in Python
Sorted list in Python
Filter List in Python
List find in Python
A collection of code often used in personal Python
[python] Get the list of classes defined in the module
[Python] Manipulation of elements in list (array) [Add / Delete]
Ruby, Python code fragment execution of selection in Emacs
One liner that outputs 1000000 digits of pi in Python
Quickly list multiple lines of text in your code
[Python] Outputs all combinations of elements in the list
Group by consecutive elements of a list in Python
How to check in Python if one of the elements of a list is in another list
[Python] Correct usage of map
Summary of Python3 list operations
[Python] Scraping in AWS Lambda
Allow brew install of command line tools made in Python
Spit out a list of file name, last modified date and character code in python3
Get the number of specific elements in a python list
Comparison of exponential moving average (EMA) code written in Python
List of nodes in diagrams
Try LINE Notify in Python
2.x, 3.x character code of python
Get index of nth largest / smallest value in list in Python
How to get a list of built-in exceptions in python
Generate QR code in Python
[Python] Copy of multidimensional list
Implementation of quicksort in Python
Getting list elements in Python
Google search for the last line of the file in Python
Character code learned in Python
Get index of nth largest / smallest value in list in Python
I tried to summarize the contents of each package saved by Python pip in one line
[Python] [3D line graph] Multiple data in one graph, axis values in characters
Create another version of Python conda environment with one command line
python Note: Determine if command line arguments are in the list
Try to get a list of breaking news threads in Python.
Mode line when you open the appropriate Python code in Vim
Read the standard output of a subprocess line by line in Python
I wrote the code to write the code of Brainf * ck in python
[python] Get the rank of the values in List in ascending / descending order
List of Linear Programming (LP) solvers and modelers available in Python
python note: map -do the same for each element of the list
Get a list of files in a folder with python without a path
Extract multiple list duplicates in Python