给定这个公式在python中实现
所以对于n = 3
x = []
y = []
for i in range(1,4):
x.append(i)
for j in range(1,4):
y.append(j**2)
total = 0
for i in range(len(x)):
for j in range(len(y)):
total = total + x[i] * y[j]
print(total)
这可行.但是说我想要第三个sigma表示法,例如
仅添加另一个循环k就可以与上述完全相同.
我的问题是,是否有一种方法可以在给定n(即j ^ 2 * i)的值的函数中对此进行概括.我陷入了更多循环泛滥的困境
def manysums(n,L : i.e. [[1,2,3],[1,4,9],8,27]]):
pass
像上面一样,列表内的值总和为
L = [[1,27],3]]
将是4 sigma表示法,这将是4个四个循环.我想知道这样的事情是否可以在函数中推广
最佳答案
请参阅
原文链接:https://www.f2er.com/python/533273.htmlitertools.product
.如果您对它的实现方式感兴趣,那么链接中有一个模拟实现.
Cartesian product of input iterables.
Roughly equivalent to nested for-loops in a generator expression. For example,
product(A,B)
returns the same as((x,y) for x in A for y in B)
.The nested loops cycle like an odometer with the rightmost element advancing on every iteration. This pattern creates a lexicographic ordering so that if the input’s iterables are sorted,the product tuples are emitted in sorted order.
from itertools import product
for i,j,k in product([1,27]):
print(j**2 * i * k**3)