0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】行列の転置

Posted at

作るもの

行列を転置する関数。

実装

transpose_matrix.py
def transposeMatrix(matrix):
    return list(zip(*matrix))

# テスト
if __name__ == "__main__":
    matrix = [[1, 2, 3], [4, 5, 6]]
    print(transposeMatrix(matrix))
出力
[(1, 4), (2, 5), (3, 6)]

補足

  • zip関数の使用例
zip.py
names = ['Alice', 'Bob', 'Charlie']
scores = [85, 92, 78]

# zip関数で対応する要素をまとめる
zipped_data = zip(names, scores)

# zipオブジェクトをリストに変換して表示
zipped_list = list(zipped_data)
print(zipped_list)
出力
[('Alice', 85), ('Bob', 92), ('Charlie', 78)]
  • アンパック演算子の使用例
unpacking_operator
coordinates = (3, 7)

x, y = coordinates  # タプルの要素を変数にアンパックする
print("x:", x)
print("y:", y)
出力
x: 3
y: 7

参考

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?