我可以使用itertools打印所有排列,但我该如何操作 [英] I can print all permutations using itertools but how do I operate

查看:81
本文介绍了我可以使用itertools打印所有排列,但我该如何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们采用使用itertools的例子,我得到了排列[1,2,3,4]

,我想添加它们。然后输出将是1 + 2 + 3 + 4即10



我尝试过:



  import  itertools  as  it 
ab = []
i 范围内( 0 ,len(a)):
ab.append([j for j in it.permutations(a,i)])



NOW WHat

解决方案

编写代码将它们添加到一起。

- 设置一个新变量来保存总数。

- 对于数组中的每个值,将其添加到总数中。


也许这样的事情:

 alist = [[ 1  2  3 ],[ 1  2 ],[ 1  3 ],[ 2  3 ]] 
x alist中的code-keyword>:
sum = 0
for y in x:
sum + = y
print (总和)


Suppose we take the example in which using itertools i get the permutation [1,2,3,4]
and i want to add them. the output would then be 1+2+3+4 that is 10

What I have tried:

import itertools as  it
ab=[]
for i in range(0,len(a)):
      ab.append([j for j in it.permutations(a,i)])


NOW WHat

解决方案

Write the code to add them together.
- Set a new variable to hold the total.
- For each value in the array add it to the total.


Something like this perhaps:

alist = [[1,2,3],[1,2],[1,3],[2,3]]
for x in alist:
	sum = 0
	for y in x:
		sum += y
	print(sum)


这篇关于我可以使用itertools打印所有排列,但我该如何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆