gpt4 book ai didi

python - 编写 python for 循环的更短方法

转载 作者:太空狗 更新时间:2023-10-30 02:29:23 25 4
gpt4 key购买 nike

在 Python 中有更短的写法吗?这里 d 是一个 python 字典,a 是一个 numpy 数组。

i = 0
for b in a:
d[b] = a[:,i]
i += 1

`

最佳答案

您可以使用enumerate 迭代器:

for i, b in enumerate(a):
d[b] = a[:,i]

因为你要进入一个字典,你可以这样做:

d.update((b, a[:,i]) for i, b in enumerate(a))

关于python - 编写 python for 循环的更短方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683162/

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