Table of Contents

List/Array

Sample

Map

def f(value) :
    return value * 2
a = [1, 2, 3]
list(map(f, a))
list(map(lambda x: (x * 2), a))
[f(x) for x in a]
[(x * 2) for x in a]
## [2, 4, 6]

Map for Lists

a = [1, 2, 3]
b = [4, 5, 6]
list(map(lambda x, y: (x * y), a, b))
## [4, 10, 18]

Map for Array / Numpy.Vectorize

a = [[1, 2], [3, 4], [5, 6]]
list(map(lambda y: list(map(lambda x: (x * 2), y)), a))
## [[2, 4], [6, 8], [10, 12]]
import numpy as np
def f(value) :
    return value * 2
vf = np.vectorize(f)
vf(a)
## array([[2, 4], [6, 8], [10, 12]])

Zip / Numpy.NdArray.T

a = [[1, 2, 3], [3, 4, 5], [5, 6, 7]]
list(zip(*a))
## [(1, 3, 5), (2, 4, 6), (3, 5, 7)]
list(map(list, zip(*a)))
## [[1, 3, 5], [2, 4, 6], [3, 5, 7]]
import numpy as np
np.asarray(a).T
## array([[1, 3, 5], [2, 4, 6], [3, 5, 7]])

Zip for Dict

keys = ['a', 'b', 'c']
values = [1, 2, 3]
list(zip(keys, values))
## [('a', 1), ('b', 2), ('c', 3)]
dict(zip(keys, values))
## {'a': 1, 'b': 2, 'c': 3}

Filter

a = [1, 2, 3, 4, 5, 6]
b = [1, 2, 3, 1, 2, 3]
def f(value) :
    return value % 2 == 0
list(filter(f, a))
list(filter(lambda x: x % 2 == 0, a))
## [2, 4, 6]
list(filter(lambda x: x[0] == x[1], zip(a, b)))
## [(1, 1), (2, 2), (3, 3)]

Enumerate

a = [1, 2, 3]
for index, value in enumerate(a) :
    print(f'{index}: {value}')
## 0: 1
## 1: 2
## 2: 3

References

https://kobazlab.tech/2019/05/10/
https://yumarublog.com/python/array-all/
https://qiita.com/ponnhide/items/c919f3bc549d1228c800
https://qiita.com/kubochiro/items/5daedd51654a8155bc06
https://deepage.net/features/numpy-transpose.html