Hi all. I'm new to the forum, this is my first post =)

I'm trying to create a list containing 3^10 lists, each list has 10 string. Each string can be of 1 or 3 types, for simplicity let them be 'a', 'b','c'.
So basically I wish to generate all possible permutations of a 10-item list, with each item being either 'a', 'b' or 'c'.

I have a script that can generate 3^3 permutations. But I realized repeating this script is highly inefficient. Is there anyway I can modify it so it's able to handle 3^10 or even bigger numbers? Code is listed below.

a = ['a','b','c']
list = []
for i in range(27):
    b = (i+3)%3
    c = (i+4)%3
    list.append([a[b],])
    if i <= (27/3)-1:
        list[i].append(a[1])
        if i <= (27/9)-1:
            list[i].append(a[1])
        elif i >= 2*(27/9)-1:
            list[i].append(a[0])
        else:
            list[i].append(a[2])
        
    elif i >= 2*(27/3)-1:
        list[i].append(a[0])
        if i <= 4*(27/9)-1:
            list[i].append(a[1])
        elif i >= 5*(27/9)-1:
            list[i].append(a[0])
        else:
            list[i].append(a[2])

    else:
        list[i].append(a[2])
        if i <= 7*(27/9)-1:
            list[i].append(a[1])
        elif i >= 8*(27/9)-1:
            list[i].append(a[0])
        else:
            list[i].append(a[2])

print list

I think what you want is called a selection ...

def selections(items, n):
    if n == 0: 
        yield []
    else:
        for i in range(len(items)):
            # recursion
            for ss in selections(items, n - 1):
                yield [items[i]] + ss

mylist = ['a','b','c']  # or list('abc')
sample = len(mylist)

# with Python3 you can use
# sel = list(itertools.product(mylist, repeat=sample))
sel = list(selections(mylist, sample))
print( len(sel) )
print( sel )

"""my result -->
27
[['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'],
['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], 
['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], 
['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], 
['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], 
['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], 
['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]
"""

Here is another method, using numbers, bases and generators

import sys
if sys.version_info[0] > 2:
    xrange = range # python 3 compatibility

def digits(n, base, how_many):
  """generate a given number of digits of n in base
  starting from the lower order digit"""
  for i in xrange(how_many):
    n, r = divmod(n, base)
    yield r

def genmaps(seq, size):
  """generate all the tupes of length size
  taking their elements in the sequence seq.
  There are len(seq) ** size tuples"""
  seq  = list(seq)
  base = len(seq)
  for n in xrange(base ** size):
    yield tuple(seq[d] for d in digits(n, base, size))

if __name__ == "__main__":
    source = genmaps("abc", 10)
    # we print only the first tuples
    # to get a list, use 'list(source)'
    for i in xrange(20):
        print(next(source))

""" my output -->
('a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('c', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('c', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('a', 'c', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('b', 'c', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('c', 'c', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('a', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('b', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('c', 'a', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('a', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('b', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('c', 'b', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('a', 'c', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('b', 'c', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('c', 'c', 'b', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('a', 'a', 'c', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
('b', 'a', 'c', 'a', 'a', 'a', 'a', 'a', 'a', 'a')
"""

Thanks Vegaseat and Gribouillis.
Yup, I should have done more searching, itertools.product works for me.
The funny thing's i looked at itertools.permute which is so close (position wise) what i was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.