promotion image of download ymail app
Promoted

Python iterable program?

list = ['a', 'a', 'a', 'b', 'b', 'c']

Using Python, how can I make an iterable program such that instead of using perm = itertools.permutations(list) which would create a lot of repeats (and 6!=720 results), how can I shorten this to no repeats, (6 choose 3)*(3 choose 2)=60 results?

1 Answer

Relevance
  • Anonymous
    1 month ago
    Favorite Answer

    1) Sort the items in the array then call the following (last is 1 past end, so put length)

    def next_permutation(array, first, last):

        if first == last:

             return False

        i = last - 1

        if first == i:

            return False

        while True:

            i1 = i

            if array[(i:=i-1)] < array[i1]:

                i2 = last

                while array[i] >= array[(i2:=i2-1)]:

                    pass

                array[i], array[i2] = array[i2], array[i]

                array[i1:last] = reversed(array[i1:last])

                return True

            if i == first:

                array[first: last] = reversed(array[first: last])

                return False

    def test(list):

        list.sort()

        print(list)

        while next_permutation(list, 0, len(list)):

            print(list)

    (p.s. I'm using the walrus operator here so you'll need the latest version of Python,

    or just modify the code to not use the walrus operator. https://www.python.org

    You can test the above code online at the above web page also.)

    test(['a', 'a', 'a', 'b', 'b', 'c'])

    ['a', 'a', 'a', 'b', 'b', 'c']

    ['a', 'a', 'a', 'b', 'c', 'b']

    ['a', 'a', 'a', 'c', 'b', 'b']

    ['a', 'a', 'b', 'a', 'b', 'c']

    ['a', 'a', 'b', 'a', 'c', 'b']

    ['a', 'a', 'b', 'b', 'a', 'c']

    ['a', 'a', 'b', 'b', 'c', 'a']

    ['a', 'a', 'b', 'c', 'a', 'b']

    ['a', 'a', 'b', 'c', 'b', 'a']

    ['a', 'a', 'c', 'a', 'b', 'b']

    ['a', 'a', 'c', 'b', 'a', 'b']

    ['a', 'a', 'c', 'b', 'b', 'a']

    ['a', 'b', 'a', 'a', 'b', 'c']

    ['a', 'b', 'a', 'a', 'c', 'b']

    ['a', 'b', 'a', 'b', 'a', 'c']

    ['a', 'b', 'a', 'b', 'c', 'a']

    ['a', 'b', 'a', 'c', 'a', 'b']

    ['a', 'b', 'a', 'c', 'b', 'a']

    ['a', 'b', 'b', 'a', 'a', 'c']

    ['a', 'b', 'b', 'a', 'c', 'a']

    ['a', 'b', 'b', 'c', 'a', 'a']

    ['a', 'b', 'c', 'a', 'a', 'b']

    ['a', 'b', 'c', 'a', 'b', 'a']

    ['a', 'b', 'c', 'b', 'a', 'a']

    ['a', 'c', 'a', 'a', 'b', 'b']

    ['a', 'c', 'a', 'b', 'a', 'b']

    ['a', 'c', 'a', 'b', 'b', 'a']

    ['a', 'c', 'b', 'a', 'a', 'b']

    ['a', 'c', 'b', 'a', 'b', 'a']

    ['a', 'c', 'b', 'b', 'a', 'a']

    ['b', 'a', 'a', 'a', 'b', 'c']

    ['b', 'a', 'a', 'a', 'c', 'b']

    ['b', 'a', 'a', 'b', 'a', 'c']

    ['b', 'a', 'a', 'b', 'c', 'a']

    ['b', 'a', 'a', 'c', 'a', 'b']

    ['b', 'a', 'a', 'c', 'b', 'a']

    ['b', 'a', 'b', 'a', 'a', 'c']

    ['b', 'a', 'b', 'a', 'c', 'a']

    ['b', 'a', 'b', 'c', 'a', 'a']

    ['b', 'a', 'c', 'a', 'a', 'b']

    ['b', 'a', 'c', 'a', 'b', 'a']

    ['b', 'a', 'c', 'b', 'a', 'a']

    ['b', 'b', 'a', 'a', 'a', 'c']

    ['b', 'b', 'a', 'a', 'c', 'a']

    ['b', 'b', 'a', 'c', 'a', 'a']

    ['b', 'b', 'c', 'a', 'a', 'a']

    ['b', 'c', 'a', 'a', 'a', 'b']

    ['b', 'c', 'a', 'a', 'b', 'a']

    ['b', 'c', 'a', 'b', 'a', 'a']

    ['b', 'c', 'b', 'a', 'a', 'a']

    ['c', 'a', 'a', 'a', 'b', 'b']

    ['c', 'a', 'a', 'b', 'a', 'b']

    ['c', 'a', 'a', 'b', 'b', 'a']

    ['c', 'a', 'b', 'a', 'a', 'b']

    ['c', 'a', 'b', 'a', 'b', 'a']

    ['c', 'a', 'b', 'b', 'a', 'a']

    ['c', 'b', 'a', 'a', 'a', 'b']

    ['c', 'b', 'a', 'a', 'b', 'a']

    ['c', 'b', 'a', 'b', 'a', 'a']

    ['c', 'b', 'b', 'a', 'a', 'a']

    test([1,3,2,2,3,3,1,1,2,2])

    [1, 1, 1, 2, 2, 2, 2, 3, 3, 3]

    [1, 1, 1, 2, 2, 2, 3, 2, 3, 3]

    [1, 1, 1, 2, 2, 2, 3, 3, 2, 3]

    [1, 1, 1, 2, 2, 2, 3, 3, 3, 2]

    [1, 1, 1, 2, 2, 3, 2, 2, 3, 3]

    [1, 1, 1, 2, 2, 3, 2, 3, 2, 3]

    (4200 lines altogether)

    [3, 3, 3, 2, 2, 1, 1, 2, 1, 2]

    [3, 3, 3, 2, 2, 1, 1, 2, 2, 1]

    [3, 3, 3, 2, 2, 1, 2, 1, 1, 2]

    [3, 3, 3, 2, 2, 1, 2, 1, 2, 1]

    [3, 3, 3, 2, 2, 1, 2, 2, 1, 1]

    [3, 3, 3, 2, 2, 2, 1, 1, 1, 2]

    [3, 3, 3, 2, 2, 2, 1, 1, 2, 1]

    [3, 3, 3, 2, 2, 2, 1, 2, 1, 1]

    [3, 3, 3, 2, 2, 2, 2, 1, 1, 1]

    • Jordan1 month agoReport

      Thanks! You're awesome!

    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.