简体   繁体   English

如何从字符串制作python 3 itertools产品/组合/排列

[英]how to make python 3 itertools product/combinations/permutations from string

Hi!你好! I want to make mixing strings program in python 3.4.我想在python 3.4中制作混合字符串程序。 Something like input: string1 = 123, string2 = abc, string3 = 4g6.类似于输入:string1 = 123,string2 = abc,string3 = 4g6。 Output should be like combine of string1, 2 and 3. Example: 1234g6abc.输出应该类似于 string1、2 和 3 的组合。例如:1234g6abc。 I tried searching in itertools and there was only combine of letters not words.我尝试在 itertools 中搜索,但只有字母组合而不是单词组合。 And i want words.我想要文字。 Please help.">请帮忙。>

Add your strings to a list, use itertools permutations , and then join the result.将您的字符串添加到列表中,使用itertools permutations ,然后加入结果。

import itertools

L = [string1, string2, string3]
# Convert perms to list if you want to iterate multiple times
perms = itertools.permutations(L)
for perm in perms:
    print(''.join(perm))
import itertools as it

>>> list(map(''.join,list(it.permutations(['234','g6','abc']))))

['234g6abc', '234abcg6', 'g6234abc', 'g6abc234', 'abc234g6', 'abcg6234']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM