Generate all possible strings from a list of token
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 开发者_如何学Csingle line.
Your example can be written in Python as
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.
itertools.permutations
can do that for you.
>>> 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
.
>>> l = ['hel', 'lo', 'bye']
>>> list(itertools.combinations(l, 2))
[('hel', 'lo'), ('hel', 'bye'), ('lo', 'bye')]
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
a = ['hel', 'lo', 'bye']
print '\n'.join(''.join(x) for x in itertools.permutations(a, 2))
Easy in python with itertools.
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)
Looks like you want 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
Python has a permutations too. :)
Update: I see I wasn't explicit enough.
Haskell has a permutations function that would help:
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 combinations of two substrings (like your example output) instead of all permutations of substrings, as @Sven noticed, use the Math.Combinatorics.Graph module and:
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.
精彩评论