Skip to content Skip to sidebar Skip to footer

Getting Possible Combinations Of A Set Of "strings" - Python

For getting possible combinations of set of 'characters', syntax would be: >>>q=[''.join(p) for p in itertools.combinations('ABC',2)] >>>q ['AB', 'AC', 'BC'] Wha

Solution 1:

just pass strings instead. Instead of iterating on each char of "ABC", combinations iterates on the strings contained in the strings list. And join acts the same (there is no difference between a character and a string in python, characters are just strings of size 1)

strings = ['A1X2','B1','C19']

q=[''.join(p) for p in itertools.combinations(strings,2)]

result:

['A1X2B1', 'A1X2C19', 'B1C19']

Post a Comment for "Getting Possible Combinations Of A Set Of "strings" - Python"