Get all combinations of a list in Python without same character repeating twice in a row
up vote
-3
down vote
favorite
Many similar questions has been asked before, but I have not found one that is the same use case as me.
So let's say I have the following list:
List = [1,2,3]
How would I print all combinations of this list, where the same number doesn't repeat twice. The output I would want is:
1
2
3
12
13
21
23
31
32
123
132
213
232
312
321
How would I achieve this?
Most solutions give outputs like:
1
2
3
12
13
32
123
132
213
232
312
321
Here 21,23 and 31 is missing, as I think it counts 12, 32 and 13 as the same combinations, which I don't want the code to do.
Thanks in advance
python python-3.x python-2.7 recursion combinations
add a comment |
up vote
-3
down vote
favorite
Many similar questions has been asked before, but I have not found one that is the same use case as me.
So let's say I have the following list:
List = [1,2,3]
How would I print all combinations of this list, where the same number doesn't repeat twice. The output I would want is:
1
2
3
12
13
21
23
31
32
123
132
213
232
312
321
How would I achieve this?
Most solutions give outputs like:
1
2
3
12
13
32
123
132
213
232
312
321
Here 21,23 and 31 is missing, as I think it counts 12, 32 and 13 as the same combinations, which I don't want the code to do.
Thanks in advance
python python-3.x python-2.7 recursion combinations
The title of your question is misleading: it says 'without character repeting twice in a row, while what you mean seems to be 'without any character repeating'
– Thierry Lathuille
Nov 21 at 9:07
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Many similar questions has been asked before, but I have not found one that is the same use case as me.
So let's say I have the following list:
List = [1,2,3]
How would I print all combinations of this list, where the same number doesn't repeat twice. The output I would want is:
1
2
3
12
13
21
23
31
32
123
132
213
232
312
321
How would I achieve this?
Most solutions give outputs like:
1
2
3
12
13
32
123
132
213
232
312
321
Here 21,23 and 31 is missing, as I think it counts 12, 32 and 13 as the same combinations, which I don't want the code to do.
Thanks in advance
python python-3.x python-2.7 recursion combinations
Many similar questions has been asked before, but I have not found one that is the same use case as me.
So let's say I have the following list:
List = [1,2,3]
How would I print all combinations of this list, where the same number doesn't repeat twice. The output I would want is:
1
2
3
12
13
21
23
31
32
123
132
213
232
312
321
How would I achieve this?
Most solutions give outputs like:
1
2
3
12
13
32
123
132
213
232
312
321
Here 21,23 and 31 is missing, as I think it counts 12, 32 and 13 as the same combinations, which I don't want the code to do.
Thanks in advance
python python-3.x python-2.7 recursion combinations
python python-3.x python-2.7 recursion combinations
asked Nov 21 at 7:29
JamesRicky
910
910
The title of your question is misleading: it says 'without character repeting twice in a row, while what you mean seems to be 'without any character repeating'
– Thierry Lathuille
Nov 21 at 9:07
add a comment |
The title of your question is misleading: it says 'without character repeting twice in a row, while what you mean seems to be 'without any character repeating'
– Thierry Lathuille
Nov 21 at 9:07
The title of your question is misleading: it says 'without character repeting twice in a row, while what you mean seems to be 'without any character repeating'
– Thierry Lathuille
Nov 21 at 9:07
The title of your question is misleading: it says 'without character repeting twice in a row, while what you mean seems to be 'without any character repeating'
– Thierry Lathuille
Nov 21 at 9:07
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Pretty sure this is a duplicate, but I cannot find a good one. itertools.permutations does what you want when you iterate over all the desired lengthes:
from itertools import permutations, chain
lst = [1,2,3]
list(chain(*(permutations(lst, n) for n in range(1, len(lst)+1))))
# [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
or:
for p in chain(*(permutations(lst, n) for n in range(1, len(lst)+1))):
print(''.join(map(str, p)))
1
2
3
12
13
21
23
31
32
123
132
213
231
312
321
this is more elegant
– Nathan McCoy
Nov 21 at 7:40
add a comment |
up vote
0
down vote
Horribly disgusting one-liner which seems to do the trick:
[
int(''.join(map(str, number)))
for n in range(1,4)
for number in itertools.product([1,2,3], repeat = n)
if not any([first == second for first, second in zip(number, number[1:])])
]
Outputs:
[1, 2, 3, 12, 13, 21, 23, 31, 32, 121, 123, 131, 132, 212, 213, 231, 232, 312, 313, 321, 323]
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Pretty sure this is a duplicate, but I cannot find a good one. itertools.permutations does what you want when you iterate over all the desired lengthes:
from itertools import permutations, chain
lst = [1,2,3]
list(chain(*(permutations(lst, n) for n in range(1, len(lst)+1))))
# [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
or:
for p in chain(*(permutations(lst, n) for n in range(1, len(lst)+1))):
print(''.join(map(str, p)))
1
2
3
12
13
21
23
31
32
123
132
213
231
312
321
this is more elegant
– Nathan McCoy
Nov 21 at 7:40
add a comment |
up vote
2
down vote
accepted
Pretty sure this is a duplicate, but I cannot find a good one. itertools.permutations does what you want when you iterate over all the desired lengthes:
from itertools import permutations, chain
lst = [1,2,3]
list(chain(*(permutations(lst, n) for n in range(1, len(lst)+1))))
# [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
or:
for p in chain(*(permutations(lst, n) for n in range(1, len(lst)+1))):
print(''.join(map(str, p)))
1
2
3
12
13
21
23
31
32
123
132
213
231
312
321
this is more elegant
– Nathan McCoy
Nov 21 at 7:40
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Pretty sure this is a duplicate, but I cannot find a good one. itertools.permutations does what you want when you iterate over all the desired lengthes:
from itertools import permutations, chain
lst = [1,2,3]
list(chain(*(permutations(lst, n) for n in range(1, len(lst)+1))))
# [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
or:
for p in chain(*(permutations(lst, n) for n in range(1, len(lst)+1))):
print(''.join(map(str, p)))
1
2
3
12
13
21
23
31
32
123
132
213
231
312
321
Pretty sure this is a duplicate, but I cannot find a good one. itertools.permutations does what you want when you iterate over all the desired lengthes:
from itertools import permutations, chain
lst = [1,2,3]
list(chain(*(permutations(lst, n) for n in range(1, len(lst)+1))))
# [(1,), (2,), (3,), (1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
or:
for p in chain(*(permutations(lst, n) for n in range(1, len(lst)+1))):
print(''.join(map(str, p)))
1
2
3
12
13
21
23
31
32
123
132
213
231
312
321
edited Nov 21 at 8:15
answered Nov 21 at 7:39
schwobaseggl
35k31937
35k31937
this is more elegant
– Nathan McCoy
Nov 21 at 7:40
add a comment |
this is more elegant
– Nathan McCoy
Nov 21 at 7:40
this is more elegant
– Nathan McCoy
Nov 21 at 7:40
this is more elegant
– Nathan McCoy
Nov 21 at 7:40
add a comment |
up vote
0
down vote
Horribly disgusting one-liner which seems to do the trick:
[
int(''.join(map(str, number)))
for n in range(1,4)
for number in itertools.product([1,2,3], repeat = n)
if not any([first == second for first, second in zip(number, number[1:])])
]
Outputs:
[1, 2, 3, 12, 13, 21, 23, 31, 32, 121, 123, 131, 132, 212, 213, 231, 232, 312, 313, 321, 323]
add a comment |
up vote
0
down vote
Horribly disgusting one-liner which seems to do the trick:
[
int(''.join(map(str, number)))
for n in range(1,4)
for number in itertools.product([1,2,3], repeat = n)
if not any([first == second for first, second in zip(number, number[1:])])
]
Outputs:
[1, 2, 3, 12, 13, 21, 23, 31, 32, 121, 123, 131, 132, 212, 213, 231, 232, 312, 313, 321, 323]
add a comment |
up vote
0
down vote
up vote
0
down vote
Horribly disgusting one-liner which seems to do the trick:
[
int(''.join(map(str, number)))
for n in range(1,4)
for number in itertools.product([1,2,3], repeat = n)
if not any([first == second for first, second in zip(number, number[1:])])
]
Outputs:
[1, 2, 3, 12, 13, 21, 23, 31, 32, 121, 123, 131, 132, 212, 213, 231, 232, 312, 313, 321, 323]
Horribly disgusting one-liner which seems to do the trick:
[
int(''.join(map(str, number)))
for n in range(1,4)
for number in itertools.product([1,2,3], repeat = n)
if not any([first == second for first, second in zip(number, number[1:])])
]
Outputs:
[1, 2, 3, 12, 13, 21, 23, 31, 32, 121, 123, 131, 132, 212, 213, 231, 232, 312, 313, 321, 323]
answered Nov 21 at 7:40
Nathan McCoy
1,0821125
1,0821125
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407145%2fget-all-combinations-of-a-list-in-python-without-same-character-repeating-twice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The title of your question is misleading: it says 'without character repeting twice in a row, while what you mean seems to be 'without any character repeating'
– Thierry Lathuille
Nov 21 at 9:07