Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list of string combinations
#1
the bash shell for Unix has a feature that allows using a text token like {a,b}{c,d}{e,f} and produces all combinations as if ace acf ade adf bce bcf bde bdf had been used in its place. has anyone seen or written a Python function that can do this to a string, maybe returning a list, such as:

string_combos('{a,b}{c,d}{e,f}') => ['ace','acf','ade','adf','bce','bcf','bde','bdf']
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It is Cartesian product of sets:

from itertools import product
list(map(''.join, product('abc', 'def','hg')))
Reply
#3
i'm looking for something that can (also) parse that particular syntax.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
What do you mean by "parse that particular syntax"?
Reply
#5
look at the function call i showed and the string being passed to the function. this is the syntax that works in bash. i want to support that syntax in programs i write that take user input.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
from itertools import product
import re

def string_combos(s):
    # get all expressions inside brackets into a list
    brackets = re.findall(r'{(.+?)}', s)

    # remove comas 
    joined_chars_list = [''.join(b.split(',')) for b in brackets]

    # In the above line the b.split(',') could be turned into another
    # list comprehension to remove whitespace if it is acceptable as
    # a part of input string

    # E.g. if this is acceptable:
    # string_combos('{a, b}{ c , d}{ e,   f}')

    # Then use something like:
    # [char_part.strip() for char_part in b.split(',')]
    
    return [''.join(p) for p in product(*joined_chars_list)] # copy of what scidam posted but I find this way easier to read than "list(map(" way
    
result = string_combos('{a,b}{c,d}{e,f}')
print(result)
Output:
['ace', 'acf', 'ade', 'adf', 'bce', 'bcf', 'bde', 'bdf']
Reply
#7
try this: print(len(string_combos('{0,1,2}'*15)))
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
What for?

Output of this line in bash:
echo {0,1,2}{0,1,2}

is the same as output of this line in the python program posted above:
string_combos('{0,1,2}{0,1,2}')

So I assume it works as expected.

'{0,1,2}'*15 in python evaluates to '{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}{0,1,2}'

The input you're suggesting to test is taking ages to compute in bash (by prepending it with 'echo'), and ages to compute in python code I posted because there's just too many combinations. I don't get what's your point...

Or did you mean to test '{0,1,2}*15' (where *15 is included within the string)?
In such case I'd suggest to use subprocess to just execute it with bash using echo.
Reply
#9
my point is diversity in testing. i guess my computer is faster or has more RAM to swap less since it only took about 3.9 seconds for me. i wanted to be sure that the code was not limited to the scope of my original example. i ran it here on Python 3.5.2 on Ubuntu 16.04.6.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding combinations of list of items (30 or so) LynnS 1 836 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  How can I find all combinations with a regular expression? AlekseyPython 0 1,636 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  All possible combinations CODEP 2 1,826 Dec-01-2020, 06:10 PM
Last Post: deanhystad
  Triplet Combinations of All Values quest 2 1,938 Nov-05-2020, 09:22 AM
Last Post: quest
  All possible combinations of multiplications Shreya10o 0 1,632 May-23-2020, 07:45 AM
Last Post: Shreya10o
  counting items in a list of number combinations Dixon 2 2,030 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Python program that list all possible football outcome combinations lukorir 5 8,774 Oct-17-2019, 04:05 AM
Last Post: steve_shambles
  Do something with all possible combinations of a list 3Pinter 7 4,014 Sep-11-2019, 08:19 AM
Last Post: perfringo
  Python to iterate a number of possible combinations teflon 4 3,884 Apr-24-2019, 03:00 AM
Last Post: scidam
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,200 Apr-07-2019, 02:34 PM
Last Post: mrapple2020

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020