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










share|improve this question






















  • 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

















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










share|improve this question






















  • 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















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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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




















  • 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














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





share|improve this answer























  • this is more elegant
    – Nathan McCoy
    Nov 21 at 7:40


















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]






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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





    share|improve this answer























    • this is more elegant
      – Nathan McCoy
      Nov 21 at 7:40















    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





    share|improve this answer























    • this is more elegant
      – Nathan McCoy
      Nov 21 at 7:40













    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





    share|improve this answer














    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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












    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]






    share|improve this answer

























      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]






      share|improve this answer























        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]






        share|improve this answer












        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]







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 7:40









        Nathan McCoy

        1,0821125




        1,0821125






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Guerrita