Generate all possible strings from a list of token

16,911

Solution 1

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.

Solution 2

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')]

Solution 3

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

Solution 4

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)

Solution 5

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
Share:
16,911
lbedogni
Author by

lbedogni

I'm Luca from Reggio Emilia, Italy. I started programming at 8 years old, beginning with QBasic and then moving to Turbo Pascal, C, PHP, Java. Now I work as a freelancer, while finishing my studies at the University of Bologna, where I got a degree in computer science and I'm attending the full-degree course. My interests are in networks.

Updated on July 21, 2022

Comments

  • lbedogni
    lbedogni almost 2 years

    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.

  • lbedogni
    lbedogni over 13 years
    Nice, but this permutes all letters on a given string, not set of strings.
  • Sven Marnach
    Sven Marnach over 13 years
    Deducing from the example, the OP actually wants combinations, not permutations.
  • kanaka
    kanaka over 13 years
    Results in: <itertools.combinations object at 0xb7d1370c>
  • Mike Axiak
    Mike Axiak over 13 years
    LucaB, permutations will work on any iterable. Look at the examples in python.
  • kanaka
    kanaka over 13 years
    Not exactly one per line, nor words.
  • kanaka
    kanaka over 13 years
    This only does 2 token combinations not all possible as requested.
  • ceth
    ceth over 13 years
    It gives all permutations of all chars.
  • kanaka
    kanaka over 13 years
    Still not one per line as requested.
  • Sven Marnach
    Sven Marnach over 13 years
    Can't find this request, even after reading the post again. Furthermore, it's rather trivial to change this.
  • Mike Axiak
    Mike Axiak over 13 years
    and yet his desired output had 2 token combinations as well :)
  • Bertrand Marron
    Bertrand Marron over 13 years
    @kanaka, It's only a matter of joining strings, printing and formatting.
  • kanaka
    kanaka over 13 years
    "but this makes permutation on a single line." implies that he wants one per line (and his example output show it as such).
  • Bertrand Marron
    Bertrand Marron over 13 years
    @kanaka, it seems to me that the OP is only asking for advices, not complete solutions.
  • LarsH
    LarsH over 13 years
    @LucaB, look again... the function permutes a list. If you pass it a list of characters (a string), it will permute those characters. If you pass it a list of strings, it will permute that list of strings.
  • LarsH
    LarsH over 13 years
    @LucaB, if you downvoted this answer because you thought the function I was talking about only worked on strings, consider changing your vote. In Haskell, strings are lists, and the function works on any list.