繁体   English   中英

以一定顺序递归洗排列表

[英]Shuffle list in certain order recursively

我正在尝试以一定顺序递归地整理列表。

我有:

def shuffle(list1, list2):
    a = []
    if len(list1) == 0:
        a += list2
        return a
    if len(list2) == 0:
        a += list1
        return a
    else:
        a += [list1[0]]
        list1.pop(0)
        a += [list2[0]]
        list2.pop(0)
    return a += shuffle(list1, list2)

您的中心问题是您没有返回递归调用。 清理代码中一些名义上未使用的本地人可以得到:

def shuffle(list1, list2):
    a = []
    if len(list1) == 0:
        return list2
    if len(list2) == 0:
        return list1
    else:
        a.append(list1.pop(0))
        a.append(list2.pop(0))
    return a + shuffle(list1, list2)

当然,在上述清理很明显,你甚至都不需要a蓄电池:

def shuffle(list1, list2):
    if len(list1) == 0:
        return list2
    if len(list2) == 0:
        return list1
    else:
        return [list1.pop(0),list2.pop(0)] + shuffle(list1, list2)

演示:

shuffle([1,2,3],[4,5,6])
Out[35]: [1, 4, 2, 5, 3, 6]

shuffle([1,2], [6,7,8,9])
Out[36]: [1, 6, 2, 7, 8, 9]

顺便说一句,这会改变输入列表,这通常是不希望的。 使用切片而不是pop元素可能会更好地为您服务:

def shuffle(list1, list2):
    if len(list1) == 0:
        return list2
    if len(list2) == 0:
        return list1
    else:
        return [list1[0],list2[0]] + shuffle(list1[1:], list2[1:])
from itertools import chain

def shuffle(list1, list2):
    if len(list1)==len(list2): return list(chain(*zip(list1,list2)))
    # if the lists are of equal length, chaining the zip will be faster
    else:
        a = []
        while any([list1,list2]):
            for lst in (list1,list2):
                try: a.append(lst.pop(0))
                except IndexError: pass
        return a
    # otherwise, so long as there's items in both list1 and list2, pop the
    # first index of each (ignoring IndexError since one might be empty)
    # in turn and append that to a, returning the value when both lists are
    # empty.

这不是您要寻找的递归解决方案,但无论如何,显式解决方案通常更快,更容易阅读和调试。 @DSM指出,这可能是一个家庭作业问题,因此对于误读我深表歉意。 我继续进行下去,以防万一。

如何使用递归模型使此函数在这些情况下起作用?

对于递归中两个列表都不为空的每个步骤,您都在创建一个新的临时数组“ a”,但随后不对其进行任何操作。

您需要通过递归链向下引用传递要存储结果的列表(首选-它是零副本),或者返回列表片段,并在展开递归时将其追加到结果数组中(功能正常,但需要一个新的列出每次要创建的对象(速度很慢)。

生成器版本,仅因为生成器很棒^^

def shuffle(a,b):
    A = iter(a)
    B = iter(b)

    while True: 
        try:
            yield A.next()
        except StopIteration:
            for j in B: 
                yield j 
            break
        try:
            yield B.next()
        except StopIteration:
            for j in A:
                yield j
            break

print list(shuffle(a,b))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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