如何在python中创建列表项的可能组合的* ALL *?

问题描述 投票:0回答:1

我需要在列表中创建ALL术语组合。我已经尝试过使用itertools的所有部分,例如排列,组合,combinations_with_replacement,但它们似乎几乎都在“计数”。例如,使用此代码:

from itertools import combinations_with_replacement
hex_chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
perm = combinations_with_replacement(hex_chars, 5) 
for i in list(perm): 
    print(i)

它产生:

('0', '0', '0', '0', '0')
('0', '0', '0', '0', '1')
('0', '0', '0', '0', '2')
('0', '0', '0', '0', '3')
('0', '0', '0', '0', '4')
('0', '0', '0', '0', '5')
('0', '0', '0', '0', '6')
('0', '0', '0', '0', '7')
('0', '0', '0', '0', '8')
('0', '0', '0', '0', '9')
('0', '0', '0', '0', 'a')
('0', '0', '0', '0', 'b')
('0', '0', '0', '0', 'c')
('0', '0', '0', '0', 'd')
('0', '0', '0', '0', 'e')
('0', '0', '0', '0', 'f')
('0', '0', '0', '1', '1')

我需要它来产生几乎所有可能的组合,例如,您会注意到它不会产生“ 000010”,并且如果放置足够长的时间,它将不会产生诸如“ A000A”之类的字符串。我需要生成字符串长度为5的所有组合(包括重复项),然后将它们全部保存到外部.txt文件中。感谢您的帮助!

python list combinations permutation itertools
1个回答
0
投票
from itertools import permutations
perm = permutations(hex_chars, 5)
while a:
    try: 
        print(next(a))
    except:
        pass
© www.soinside.com 2019 - 2024. All rights reserved.