簡體   English   中英

如何從字符串制作python 3 itertools產品/組合/排列

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

你好! 我想在python 3.4中制作混合字符串程序。 類似於輸入:string1 = 123,string2 = abc,string3 = 4g6。 輸出應該類似於 string1、2 和 3 的組合。例如:1234g6abc。 我嘗試在 itertools 中搜索,但只有字母組合而不是單詞組合。 我想要文字。 請幫忙。>

將您的字符串添加到列表中,使用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