I have integer for example "123", using this i want to create all possible combinations listed below. 123 12,3 1,23 and so on irrespective of the digits i have entered. Is there any way possible using python for the same? I am not able to get any idea.
Asked
Active
Viewed 1,839 times
-4
-
1Have you looked at the itertools module and the combinations/permutations functions? – Jon Clements Jun 07 '17 at 07:49
-
Yes itertools gives me an output like "['123', '132', '213', '231', '312', '321']". But i need a output of 123 to be like (1,2,3) , (12,3), (1,23) and so on – sKen Jun 07 '17 at 07:57
-
2Your question is currently too vague. Is order significant? Should the output contain (1,32)? Should it contain (1,2,3) and (3,1,2)? – PM 2Ring Jun 07 '17 at 08:21
-
1Are you asking for all partitions of the set of numbers `1,2,3`? https://stackoverflow.com/questions/30893292/generate-all-partitions-of-a-set – juanpa.arrivillaga Jun 07 '17 at 08:29
2 Answers
0
This would work:
import itertools
stuff = list('123')
for L in range(0, len(stuff)+1):
for subset in itertools.combinations(stuff, L):
print(subset)
Output:
()
(1,)
(2,)
(3,)
(1, 2)
(1, 3)
(2, 3)
(1, 2, 3)

Jeril
- 7,858
- 3
- 52
- 69
0
I guess this is what you are looking for:
num = 123
s = str(num)
[[x for x in i if x is not '' and ' '] for i in [list(s.partition(item)) for item in list(s+' ')]]
Output:
[['1', '23'], ['1', '2', '3'], ['12', '3'], ['123']]
You could also use tuples:
s = str(num)
[tuple([x for x in i if x is not '' and ' ']) for i in [list(s.partition(item)) for item in list(s+' ')]]
Output:
[('1', '23'), ('1', '2', '3'), ('12', '3'), ('123',)]

nikpod
- 1,238
- 14
- 22
-
This is probably what the OP wants, but guessing can be dangerous if you guess wrong. ;) BTW, it's generally not a good idea to use `is` to test strings, since `is` tests identity, not value. It works ok here because CPython optimizes the empty string, and it will often work with short strings that get re-used by the interpreter, but it won't always work. See [Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?](https://stackoverflow.com/q/1504717/4014959) for further details. – PM 2Ring Jun 07 '17 at 09:17
-
1BTW, `if x is not '' and ' '` is interpreted as `if (x is not '') and ' '`, and therefore it's logically equivalent to `if x`. The empty string has a boolean value of `False`, and any other string has a boolean value of `True`. So if `x` is the empty string, `x is not ''` evaluates to `False`, so the `if` test fails. Otherwise, `x is not ''` evaluates to `True`, the second operator of `and` (`' '`) becomes the value of the expression, so the `if` test passes. – PM 2Ring Jun 07 '17 at 09:26
-
Thanks, I did not notice the `if (x is not '') and ' '` part. Although surprisingly, my code did remove the trailing and leading `''` and `' '` just the way I needed. – nikpod Jun 07 '17 at 10:07
-
But what if there's '123' as the input and apart from the outputs obtained above, I also need to get ['2','13'] as part of the output? any suggestions. i'm basically stuck trying to figure something out here. – gireesh4manu Feb 20 '19 at 06:49
-
-
1@gireesh4manu If none of the existing questions do what you want you should ask a new question. Clearly state the full output you want for the '123' input, and include your own code attempt (otherwise your question may be closed). – PM 2Ring Feb 20 '19 at 07:32