python - Shuffle list in certain order recursively -
I am trying to revalue the list in a certain order.
DIFFFLAM (list1, list2): A = [] if LAN (list1) == 0: a + = list2 if a returned lane (List2) == 0 : A + = list1 one more return: a + = [list1 [0]] list1.pop (0) a + = [list2 [0]] list2.pop (0) a + = alteration (list1, list2 )
Your central problem is that you are not returning your recurring call. Cleaned up some nominal-unused locals in your code:
def shuffle (list1, list2): a = [] if len (list1) == 0: return list2 if len (List2) == 0: return list 1 other: a.append (list1.pop (0)) a.append (list2.pop (0)) return a + alteration (list1, list2)
< In the above cleaning it is clear that you do not even need aa
accumulator:Diff Falpham (list1, list2): If LAN (list1) == 0: return list 2 if lane (list2) == 0: return list 1 other: return [list 1.pop (0), list 2.pop (0)] + firbad (List 1, List 2)
Demo:
Shuffle ([1,2,3], [4,5,6]) Outside [ 35]: [1, 4, 2, 5, 3, 6] reshuffle ([1,2], [6,7,8,9]) out [36]: [1, 6, 2, 7, 8, 9]
On the one hand, it changes the input lists, which is generally not desirable. Instead of closing ping elements, you can be better served:
def shuffle (list1, list2): if len (list1) = = 0: return list 2 if LAN (list2) == 0: return list 1 other: return [list 1 [0], list 2 [0]] + alteration (list 1 [1:], list 2 [1:] )
Comments
Post a Comment