简体   繁体   中英

Why doesn't this modified Cartesian Product function for Python work?

Ideally, the input is [1,2], and the output is all combinations [[1,1], [2,2], [1,2], [2,1]]. Basically, print all possible combinations with replacement.

def cart(lst):
   if lst == []:
      return [[]]

   return [x[i:] + [lst[0]] + x[:i] for x in cart(lst[1:]) for i in range(len(x)) ]

l = [1,2,3] 
print cart(l)

Returns

[]

In more human-readable form, the code basically says:

for x in cart(lst[1:]):
   for i in range(len(x)):
      return x[i:] + [lst[0]] + x[:i]

And if we assume the recursive case with input [1,2,3] , then cart([2,3]) should produce [[2,3], [3,2], [2,2], [3,3]] , and so for the recursive step we would want to insert 1 in every possible location. (This code might be missing the 111 case.)

The code appears logically correct, but outputs an empty string.

Is there something missing or am I approaching the problem incorrectly?

Edit

Actually, I realize the code would be slightly more complicated:

def cart(lst):
    if len(lst) <= 1:
        return lst
    else:
        return [x[i:] + [lst[j]] + x[:i] for x in cart(lst[1:]) for j in range(len(lst)) for i in range(len(x))]

Although this still strangely returns an empty list. My hunch is that I am missing a base case.

Edit

It was something to do with my base case. Revised code:

def cart(lst):
    if len(lst) <= 1:
        return [lst]
    else:
        return [x[i:] + [lst[j]] + x[:i] for x in cart(lst[1:]) for j in range(len(lst)) for i in range(len(x))]

l = [1,2,3]
print cart(l)

But now returns

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

Better now, although the output is missing sets. Seems like a base case issue again.

Found the answer here Algorithm for recursive function for permutations with replacement in python

def permutations_with_replacement(k,n):
         # special case (not part of recursion)
         if k == 0:
            return []

         if k == 1:
            return [[n[i]] for i in range(len(n))]

         else:
            # Make the list by sticking the k-1 permutations onto each number 
            # we can select here at level k    
            result = []
            # Only call the k-1 case once, though we need it's output n times.
            k_take_one_permutations = permutations_with_replacement(k-1,n)  

            for i in range(len(n)):
                for permutation in k_take_one_permutations:
                    result.append([n[i]]+permutation)   
            return result

         print permutations_with_replacement(3,2)

print permutations_with_replacement(3, [1,2,3])

It appears I was trying to take the recursive case of the list itself, rather than the size of the combinations.

I'm wondering whether or not the solution would be do-able by recurring on the list, rather than the size of the combinations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM