1

I'm in trouble creating a combination of elements from list.

What i would like to do is to create a recursive function in Python which returns a combination of elements for example list a = [1,2,3,4,5,6,7,8] and a result will be combinations [1,2,3,4],[1,3,4,5],[1,4,5,6],[1,2,4,5] etc. For 8 elements it should return 70 combinations (if i did my math right). Although the best option would be that the combinations don't repeat.

I tried to code it, but what i get is only [1,2,3,4],[1,3,4,5] etc but not combination [1,5,7,8]

I know there is a special function but i'd like to do it recursively. Any suggestions?

nimed = ["A","B","C","D","E","F","G","H"]


def kombinatsioonid(listike,popitav):
  if len(listike) < 4:
      return
  tyhi = []
  for c in range(len(listike)):
      tyhi.append(listike[c])
  listike.pop(popitav)
  print(tyhi)
  kombinatsioonid(listike,popitav)

kombinatsioonid(nimed,1) 
charen
  • 371
  • 1
  • 7
  • 20
  • 1
    Waht is the difference between your output and your wanted output – Vincent Beltman Oct 01 '14 at 08:00
  • Well, for example i don't get combinations like [A,E,G,H] or for like done above [1,5,7,8] – charen Oct 01 '14 at 08:03
  • Wouldn't it be better just to use [itertools.permutations()](https://docs.python.org/2/library/itertools.html#itertools.permutations)? – Kobi K Oct 01 '14 at 08:04
  • It would be great indeed but i'm doing this as a school project which particularly requires us not to use itertools. This is just a part of the bigger program but i'm stuck in that point currently. – charen Oct 01 '14 at 08:06
  • 1
    "Just an edit" Change your forloop to this: for c in listike:tyhi.append(c) – Vincent Beltman Oct 01 '14 at 08:10
  • The code you've shown would not print `[1,2,3,4]` or `[1,3,4,5]`. All it does is repeatedly remove the second element until there are four left. Also, it isn't clear exactly what you're trying to do. Do you want to output only combinations of length 4? – interjay Oct 01 '14 at 08:11
  • Interjay, yes - sorry about the misunderstanding, English is not my native language and at some point i may sound not understandable. The code i added is buggy indeed but this was a shown example of what i've done. And as i've said before: i would like to create combinations of lenght 4 from the list (which currently has 8 elements). – charen Oct 01 '14 at 08:15

2 Answers2

2

This can be done in this way :

def combination(l,n, mylist=[]):
    if not n:  print(mylist)
    for i in range(len(l)):
        mylist.append(l[i])
        combination(l[i+1:], n-1, mylist)
        mylist.pop()

l = ["A","B","C","D","E","F","G","H"]
n=4
combination(l, n)
bluefoggy
  • 961
  • 1
  • 9
  • 23
1

For each element x in a, generate all k-1 combinations from the elements right to it, and prepend x to each one. If k==0, simply return one empty combination, thus exiting the recursion:

def combs(a, k):
    if k == 0:
        return [[]]
    r = []
    for i, x in enumerate(a):
        for c in combs(a[i+1:], k - 1):
            r.append([x] + c)
    #print '\t' * k, k, 'of', a, '=', r
    return r

Uncomment the "print" line to see what's going on.

As a side note, it's better to use English variable and function names, just for the sake of interoperability (your very question being an example).

georg
  • 211,518
  • 52
  • 313
  • 390