如何在Python中重复获取所有组合 [英] How get all combinations at python with repeat

查看:86
本文介绍了如何在Python中重复获取所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码示例

from itertools import *
from collections import Counter
from tqdm import *
#for i in tqdm(Iterable):
for i in combinations_with_replacement(['1','2','3','4','5','6','7','8'], 8):
    b = (''.join(i))
    if b == '72637721':
        print (b)

当我尝试我拥有的东西

    for i in product(['1','2','3','4','5','7','6','8'], 8):
TypeError: 'int' object is not iterable

我如何获得所有组合? (我相信它是未经测试的,所以现在我所做的一切都是错误的)

How can i get all combinations ? ( i was belive it before not test , so now all what i was do wrong)

我听说过combs_with_replacement返回all,但是我怎么看这是谎言

i was read about combinations_with_replacement return all , but how i see it's lie

我使用python 3.8

i use python 3.8

提出要问

11111111 11111112 11111113 11111114 11111115 11111116 11111117 11111118 11111122 11111123 11111124 11111125 11111126 11111127 11111128 11111133 11111134 11111135 11111136 11111137 11111138 11111144 11111145 11111146 11111147 11111148 11111155 11111156 11111157 11111158 11111166 11111167 11111168 11111177 11111178 11111188 11111222 11111223 11111224 11111225 11111226 11111227 11111228 11111233 11111234 11111235 11111236 11111237 11111238 11111244 11111245 11111246 11111247 11111248 11111255 11111256 11111257 11111258 11111266 11111267 11111268 11111277 11111278 11111288

11111111 11111112 11111113 11111114 11111115 11111116 11111117 11111118 11111122 11111123 11111124 11111125 11111126 11111127 11111128 11111133 11111134 11111135 11111136 11111137 11111138 11111144 11111145 11111146 11111147 11111148 11111155 11111156 11111157 11111158 11111166 11111167 11111168 11111177 11111178 11111188 11111222 11111223 11111224 11111225 11111226 11111227 11111228 11111233 11111234 11111235 11111236 11111237 11111238 11111244 11111245 11111246 11111247 11111248 11111255 11111256 11111257 11111258 11111266 11111267 11111268 11111277 11111278 11111288

它从开始到结束都给出了

what it start give at end

56666888 56668888 56688888 56888888 58888888 77777777 77777776 77777778 77777766 77777768 77777788 77777666 77777668 77777688 77777888 77776666 77776668 77776688 77776888 77778888 77766666 77766668 77766688 77766888 77768888 77788888 77666666 77666668 77666688 77666888 77668888 77688888 77888888 76666666 76666668 76666688 76666888 76668888 76688888 76888888 78888888 66666666 66666668 66666688 66666888 66668888 66688888 66888888 68888888 88888888

56666888 56668888 56688888 56888888 58888888 77777777 77777776 77777778 77777766 77777768 77777788 77777666 77777668 77777688 77777888 77776666 77776668 77776688 77776888 77778888 77766666 77766668 77766688 77766888 77768888 77788888 77666666 77666668 77666688 77666888 77668888 77688888 77888888 76666666 76666668 76666688 76666888 76668888 76688888 76888888 78888888 66666666 66666668 66666688 66666888 66668888 66688888 66888888 68888888 88888888

更多cleare认为它是如何从1111 1111到8888 8888进行计数的(但是对于字符,所以这就是为什么我使用try在置换/组合并复制时使用它的原因... 它会错过这些符号的某些可能组合.

more cleare think it how it be count from 1111 1111 to 8888 8888 ( but for characters , so this why i use try do it at permutation/combine with repitions... it miss some possible combinations of that symbols.

例如,我尝试做的事情是,对十六进制数字的所有可能变体(例如从0到F)进行所有排列,但不仅要针对它们,还应使之适用于任何字符.

as example what i try do , make all permutatuion of possible variants of hex numbers , like from 0 to F , but make it not only for them , make this possible for any charater.

仅在示例['1','2','3','4','5','6','7','8'] 可以是['a','b','x','c','d','g','r','8']等.

this only at example ['1','2','3','4','5','6','7','8'] this can be ['a','b','x','c','d','g','r','8'] etc.

推荐答案

解决方案是使用itertools.product代替groups_with_replacement

solition is use itertools.product instead combinations_with_replacement

from itertools import *
for i in product(['1','2','3','4','5','6','7','8'],repeat = 8):
    b = (''.join(i))
    if b == '72637721':
        print (b)

:

itertools.product ('ABCD', 'ABCD') AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD # full multiplication with duplicates and mirrored pairs 

itertools.permutations ('ABCD', 2) -> AB AC AD BA BC BD CA CB CD DA DB DC # full multiplication without duplicates and mirrored pairs 

itertools.combinations_with_replacement ('ABCD', 2) -> AA AB AC AD BB BC BD CC CD DD # no mirror pairs with duplicates 

itertools.combinations ('ABCD', 2) -> AB AC AD BC BD CD # no mirrored pairs and no duplicates

这篇关于如何在Python中重复获取所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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