2

although there's quite a bit of information about recursion on the web, I haven't found anything that I was able to apply to my problem. I am still very new to programming so please excuse me if my question is rather trivial.

Thanks for helping out :)

This is what I want to end up with:

listVariations(listOfItems, numberOfDigits) 

>>> listVariations(['a', 'b', 'c'], 1)
>>> ['a', 'b', 'c'] 

>>> listVariations(['a', 'b', 'c'], 2)
>>> ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

>>> listVariations(['a', 'b', 'c'], 3)
>>> ['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc', 'baa', 'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa', 'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']

but so far I was only able to come up with a function where I need to specify/know the number of digits in advance. This is ugly and wrong:

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

def listVariations1(list):
  variations = []
  for i in list:
    variations.append(i)
  return variations

def listVariations2(list):
  variations = []
  for i in list:
    for j in list:
      variations.append(i+j)
  return variations

def listVariations3(list):
  variations = []
  for i in list:
    for j in list:
      for k in list:
        variations.append(i+j+k)
  return variations

oneDigitList = listVariations1(list)
twoDigitList = listVariations2(list)
threeDigitList = listVariations3(list)

This is probably very easy, but I couldn't come up with a good way to concatenate the strings when the function calls itself.

Thanks for your effort :)

2
  • 3
    You really shouldn't use list as a variable name. It's the constructor for the builtin list class and when you do this, you shadow it. Commented Nov 26, 2010 at 11:24
  • point taken - won't happen again... Commented Nov 26, 2010 at 12:18

3 Answers 3

5

You can use the product() function in itertools

Sign up to request clarification or add additional context in comments.

2 Comments

list(''.join(item) for item in itertools.product(list_, repeat=3))
this is exactly what I was looking to achieve. thanks guys :)
0
from itertools import combinations_with_replacement

This function does exactly what you want.

1 Comment

This only works in python 3.1 or greater. And isn't right anyways. Srinivas Reddy Thatiparth gave the right answer.
0

You can use itertools.permutations to do this.

from itertools import permutations

def listVariations(listOfItems, numberOfDigits):
    return [''.join(x) for x in permutations(listOfItems, numberOfDigits)]

If you do want to implement something similar with recursive function call, you can do it like this:

def permute(seq, n):
    for i in xrange(len(seq)):
        head, tail = seq[i:i+1], seq[0:i]+seq[i+1:]
        if n == 1: 
            yield head
        else:
            if tail:
                for sub_seq in permute(tail, n-1):
                    yield head + sub_seq
            else:
                yield head

a_list = ['a', 'b', 'c']
list(permute(''.join(a_list), 2))
a_str = 'abc'
list(permute(a_str, 2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.