0

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

What about getting possible combinations of set of "STRINGS" for example :

'A1X2','B1','C19'

output should be: ['A1X2B1', 'A1X2C19', 'B1C19']

1 Answer 1

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.