from itertools import permutations,combinations,product#方法一和方法二,均实现了,输入一个字符串,输出各种排列,不考虑顺序 strs = 'abc' #方法1 def zuHe(strs):n = len(strs)if not n:return strslts = []for i in range(1,n+1):for j in permutations(strs,i):flage = True #定义一个旗帜for k in permutations(j): #检查全排列中的某一种,是否已经存在于lts中,因为不考虑顺序# for k in permutations(j,i): #permutations,不加参数i,默认就是全排列k_strs = ''.join(k)if k_strs in lts:flage = Falsebreakif flage:j_strs = ''.join(j)lts.append(j_strs)return lts#方法2 def zuHe2(strs):n = len(strs)if not n:return strslts = []for i in range(1,n+1):for j in combinations(strs,i):flage = True #定义一个旗帜# 检查全排列中的某一种,是否已经存在于lts中,因为不考虑顺序 combinations需要指定参数ifor k in combinations(j,i):k_strs = ''.join(k)if k_strs in lts:flage = Falsebreakif flage:j_strs = ''.join(j)lts.append(j_strs)return lts#笛卡尔,多用在组合矩阵方面 for m in product([1,2],repeat=3):print(m)''' (1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 2, 2) (2, 1, 1) (2, 1, 2) (2, 2, 1) (2, 2, 2) ''' if __name__ == '__main__':print(zuHe(strs)) #['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc']print(zuHe2(strs)) #['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc']
详细解决方案
itertools模块中,permutations,combinations和product的使用
热度:51 发布时间:2023-12-27 23:54:02.0
相关解决方案
- 在类名上这样写是啥[Table(Name = "Product")]解决方法
- unknown build 有关问题 -eclipse product
- 更换RCP product 之exe执行图标解决办法
- E:\oracle\product\10.2.0\oradata\wzinner\里的ora资料太大
- 求随机展示SELECT TOP 5 * FROM Product ORDER BY NEWID()的完整代码以及使用
- 为何在查询分析器里做象select productname from product where productname>'',该如何处理
- 求android2.3.5里面的build/target/product/security/platform.x509.pem跟platform.pk8两个文件
- 针对android4.0之上版本的源码编译的out/target/product/./obj文件的作用
- 国内项目的Scrum实践- 怎么做PB(product backlog)
- android 源码上编译自己的项目,mm时提示make: * 没有规则可以创建.需要的目标“out/target/product/generic/ob
- [Android.mk]build\core\product.mk下的_product_stash_var_list作用,该如何处理
- 开启site collection feature,提醒“The trial period for this product has expired”
- 【SQL】安装 SQL SERVER MsiGetProductInfo 无法检索 Product Code 1605异常 解决方案
- (置换群)poj2369?Permutations
- Codeforces Round #496 (Div. 3) E题 Median on Segments (Permutations Edition)
- 47. Permutations II
- CodeForces 463D Gargari and Permutations (DFS建图+DP)
- leetcode 238 Product of Array Except Self
- 欧拉计划24--Lexicographic permutations
- 欧拉计划008--Largest product in a series
- 欧拉计划004--Largest palindrome product
- I - Combinations
- itertools.chain.from_iterable用法
- Minimum Scalar product
- wincvs的“License for this product has expired”问题解决
- UVa 11509 Maximum Product(最大乘积)
- Codeforces 501D Misha and Permutations Summation (康托展开+平衡树优化)
- Div3 codeforces1005E1 Median on Segments (Permutations Edition)
- 最小标量积 Minimun Scalar Product(挑战程序设计竞赛)
- leetcode | Letter Combinations of a Phone Number