如何创建wordlist矩阵

时间:2018-05-21 23:31:39

标签: python list

我希望根据单词列表创建一个连接列表。说多达10个字符串并创建它们的每个组合。例如,以下列表:

asdf 
1234 
jkl;

会创建:

asdf
1234
jkl;
asdf1234
asdf1234jkl;
1234jkl;asdf
etc.

这在Python中可行吗?我搜索过,我找不到这个。我应该搜索什么?

1 个答案:

答案 0 :(得分:0)

Python解决方案

您可以使用列表推导来迭代itertools.permutations的结果并使用str.join

from itertools import permutations

lst = ['asdf', '1234', 'jkl;']

res = [''.join(p) for i in range(1, len(lst)+1) for p in permutations(lst, i)]

print(res)

['asdf',
 '1234',
 'jkl;',
 'asdf1234',
 'asdfjkl;',
 '1234asdf',
 '1234jkl;',
 'jkl;asdf',
 'jkl;1234',
 'asdf1234jkl;',
 'asdfjkl;1234',
 '1234asdfjkl;',
 '1234jkl;asdf',
 'jkl;asdf1234',
 'jkl;1234asdf']