加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

多个sigma表示法python

发布时间:2020-12-17 17:40:53 所属栏目:Python 来源:网络整理
导读:给定这个公式在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 = 0for i in range(len(x)): for j in range(len(y)): total = total + x[i] * y[j] print(total) 这可行.但是说我想要第

给定这个公式在python中实现

enter image description here

所以对于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表示法,例如

enter image description here

仅添加另一个循环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个四个循环.我想知道这样的事情是否可以在函数中推广

最佳答案
请参阅itertools.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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读