gpt4 book ai didi

python - 如何迭代矩阵的列?

转载 作者:IT老高 更新时间:2023-10-28 22:21:41 25 4
gpt4 key购买 nike

如果在 python 中定义:

a = arange(9).reshape(3,3)

作为 3x3 矩阵并进行迭代:

for i in a:

它将遍历矩阵的行。有什么方法可以遍历列吗?

最佳答案

怎么样

for i in a.transpose():

或者,更短:

for i in a.T:

这可能看起来很昂贵,但实际上非常便宜(它返回相同数据的 View ,但形状和步幅属性已置换)。

关于python - 如何迭代矩阵的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5515181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com