简体   繁体   English

在Python中的嵌套列表中循环

[英]loop in nested list in python

Hello I am new in Python and what I am trying to do is to make a loop in a nested list to sum the elements 10 by 10 in the code below: 您好,我是Python的新手,我想做的是在嵌套列表中进行循环,以在下面的代码中将元素乘以10乘10:

1.1 1.1

for i in range (n):
            for k in range (n):
                G[i] += (F[(n*i)+k])

and I am getting the error: 我收到错误:

ValueError: operands could not be broadcast together with shapes (6,) (3,) ValueError:操作数不能与形状(6,)(3,)一起广播

what I already did and worked was 我已经做过的工作是

1.2 1.2

for i in range (n):
    G[i]=F[n*i+0]+F[n*i+1]+F[n*i+2]+F[n*i+3]+F[n*i+4]+F[n*i+5]+F[n*i+6]+F[n*i+7]+F[n*i+8]+F[n*i+9]

So I am trying to do the loop to get the same as in 1.2 but I know i cant use the loop in 1.1 in python. 所以我试图做循环以获得与1.2中相同,但我知道我无法在python中使用1.1中的循环。 How would I do that? 我该怎么做? Thanks a lot! 非常感谢!

I am not sure of what you want to add. 我不确定您要添加什么。 If it is a problem with the shape of the array, it should be 如果数组的形状有问题,应该

import numpy as np
n=10
t=[(0,0,0)]
F=np.array(t*n**2)
G=np.array(t*n)
for i in range (n):
    for k in range (n):
        G[i] += F[n*i+k]

Of course if t=[(0,0,0)], it sums up to zero 当然,如果t = [(0,0,0)],则总和为零

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM