【发布时间】:2021-12-10 00:36:26
【问题描述】:
我想对一个由其他子数组组成的数组求和。
import numpy as np
a1 = np.array([1,2,3])
a2 = np.array([3,4,5])
a3 = np.array([6,7,8])
a = np.concatenate(([a1],[a2],[a3]))
print(len(a))
for i in range(1,len(a)): (1)
for j in i : (2)
for k in j : (3)
goal = np.sum(np.cos(k))
print(goal)
goal to achieve :
goal = cos(1)+cos(2)+cos(3)
goal = cos(3)+cos(4)+cos(5)
goal = cos(6)+cos(7)+cos(8)
我想先在包含所有其他子数组 (1) 的数组上循环,然后循环每个子数组 (2),最后在每个子数组 (3) 内循环。
我会得到回溯:
File "*****************", line 18, in <module>
for j in i :
TypeError: 'int' object is not iterable
在我真正的问题中,我可能有超过 3 个子数组。我认为可以做到,但我仍在探索 Python。
【问题讨论】:
标签: python numpy loops for-loop