bash 从令牌列表中生成所有可能的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4059550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:50:42  来源:igfitidea点击:

Generate all possible strings from a list of token

pythonbashlanguage-agnostic

提问by lbedogni

I have a list of tokens, like:

我有一个令牌列表,例如:

hel
lo
bye

and i want to generate all the possible combinations of such strings, like:

我想生成此类字符串的所有可能组合,例如:

hello
lohel
helbye
byehel
lobye
byelo

Language is not important, any advice?

语言不重要,有什么建议吗?

I found Generating permutations using bash, but this makes permutation on a single line.

我发现Generating permutations using bash,但这在一行中进行了排列。

回答by Sven Marnach

Your example can be written in Python as

您的示例可以用 Python 编写为

from itertools import combinations
print list(combinations(["hel", "lo", "bye"], 2))

To combine the output to strings again:

再次将输出合并为字符串:

print ["".join(a) for a in combinations(["hel", "lo", "bye"], 2)]

If you interested in the actual implementation of this function, have a look at the documentation.

如果您对此功能的实际实现感兴趣,请查看文档

回答by Bertrand Marron

itertools.permutationscan do that for you.

itertools.permutations可以为你做到这一点。

>>> l = ['hel', 'lo', 'bye']
>>> list(itertools.permutations(l, 2))
[('hel', 'lo'), ('hel', 'bye'), ('lo', 'hel'), ('lo', 'bye'), ('bye', 'hel'), ('bye', 'lo')]

Or if you want combinations, you can use itertools.combinations.

或者,如果您想要组合,您可以使用itertools.combinations.

>>> l = ['hel', 'lo', 'bye']
>>> list(itertools.combinations(l, 2))
[('hel', 'lo'), ('hel', 'bye'), ('lo', 'bye')]

回答by Sinan ünür

Given that other languages are acceptable:

鉴于其他语言是可以接受的:

#!/usr/bin/perl

use strict; use warnings;
use Algorithm::Combinatorics qw(permutations);

my $data = [ qw( hel lo bye ) ];
my $it = permutations($data);

while ( my $p = $it->next ) {
    print @$p, "\n";
}
hellobye
helbyelo
lohelbye
lobyehel
byehello
byelohel

回答by Mike Axiak

a = ['hel', 'lo', 'bye']
print '\n'.join(''.join(x) for x in itertools.permutations(a, 2))

回答by kanaka

Easy in python with itertools.

使用 itertools 在 python 中很容易。

Here is the token permutation example:

这是令牌排列示例:

import itertools

tokens = ["hel", "lo", "bye"]

for i in range(1, len(tokens) + 1):
    for p in itertools.permutations(tokens, i):
        print "".join(p)

Alternatively, this treats each character as a token:

或者,这将每个字符视为一个标记:

import itertools

tokens = ["hel", "lo", "bye"]

chars = "".join(tokens)
for i in range(1, len(chars) + 1):
    for p in itertools.permutations(chars, i):
        print "".join(p)

回答by demas

Python has a permutationstoo. :)

Python 也有排列。:)

回答by Mark Tolonen

Looks like you want permutations:

看起来你想要permutations

from itertools import permutations

# easy way to make a list for words
words = 'hel lo bye'.split()

# fetch two-word permutations, joined into a string
for word in [''.join(s) for s in permutations(words,2)]:
    print word

Output:

输出:

hello
helbye
lohel
lobye
byehel
byelo

回答by LarsH

Update:I see I wasn't explicit enough.

更新:我发现我不够明确。

Haskell has a permutationsfunction that would help:

Haskell 有一个排列函数可以帮助:

import Data.List
permutations ["hel","lo","bye"] ==
[["hel","lo","bye"],["lo","hel","bye"],["bye","lo","hel"],
 ["lo","bye","hel"],["bye","hel","lo"],["hel","bye","lo"]]

If you want each permutation concatenated, use

如果您希望连接每个排列,请使用

map concat (permutations ["hel","lo","bye"]) ==
["hellobye","lohelbye","byelohel","lobyehel","byehello","helbyelo"]

If you actually want combinationsof two substrings (like your example output) instead of all permutationsof substrings, as @Sven noticed, use the Math.Combinatorics.Graph module and:

如果您确实想要两个子字符串的组合(如您的示例输出)而不是子字符串的所有排列,如@Sven 所注意到的,请使用 Math.Combinatorics.Graph 模块和:

map concat (combinationsOf 2 ["hel","lo","bye"])

That matches your example data in some respects but not others. I could go on to speculate that you want "all possible strings" as the title says, or all permutations of two-token subsets, or what have you, but it's kind of pointless to speculate since you've already accepted an answer.

这在某些方面与您的示例数据匹配,但在其他方面不匹配。我可以继续推测您想要标题所说的“所有可能的字符串”,或者两个标记子集的所有排列,或者你有什么,但推测是没有意义的,因为你已经接受了一个答案。