0

More than 1 year has passed since last update.

# Python数学シリーズ① 転置

Last updated at Posted at 2020-12-21

## 転置

### 実装

main.py
``````import numpy

def main():
l_2d = [[0, 1, 2], [3, 4, 5]]
arr_t = np.array(l_2d).T.tolist()
print(l_2d)
# [[0 3]
#  [1 4]
#  [2 5]]
``````

``````>>> l_2d = [[0, 1, 2], [3, 4, 5]]
>>> [*zip(*l_2d)]
[(0, 3), (1, 4), (2, 5)]
>>> list(map(list, zip(*l_2d)))
[[0, 3], [1, 4], [2, 5]]
>>> [[*row] for row in zip(*l_2d)]
[[0, 3], [1, 4], [2, 5]]
>>> [list(row) for row in zip(*l_2d)]
[[0, 3], [1, 4], [2, 5]]
``````

main.py
``````def transpose(arr):
col = len(arr[0]) # この場合は3
row = len(arr)  # この場合は2
ans_list = []
i = 0
while i < col:
tmp_list = []
j = 0
while j < row:
tmp_list.append(arr[j][i])
j += 1
ans_list.append(tmp_list)
i += 1
return ans_list

def main():
l_2d = [[0, 1, 2], [3, 4, 5]]
print(transpose(l_2d))
# [[0 3]
#  [1 4]
#  [2 5]]
``````

``````def transpose(arr):
col = len(arr[0]) # この場合は3
row = len(arr)  # この場合は2
rows = []
for c in range(col):
cols = []
for r in range(row):
cols.append(arr[r][c])
rows.append(cols)
return rows
``````

## まとめ

こんな感じでゆる~く深層学習の本を読み進めていこうと思います。

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
What you can do with signing up
0