[英]how to use itertools methods so that they show all possible combinations
我需要找到絕對所有可能的組合,包括考慮到他們的 position。 例如,組合 222、200、220 不會以任何方式出現。 還有什么可以做的?
這是mu代碼:
import itertools
real_code = '220'
code = itertools.permutations('012', r=3)
set_of_code = set()
for i in code:
set_of_code.add(''.join(i))
# print('permut', i)
code2 = itertools.combinations_with_replacement('012', r=3)
for i in code2:
set_of_code.add(''.join(i))
# print('comboW', i)
for i in set_of_code:
if i == real_code:
print('found', i)
break
else:
print('not found')
這應該為您提供所需的組合:
code = itertools.product('012', repeat=3)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.