2

我想知道,是否有办法加快此处显示的功能。我知道这看起来不是很蟒蛇......

def MakePairs(inputlist):
   '''
    @param inputlist: [[["a","b","c"],["d","e","f"]],[["g","h","i"],["j","k","l"]],...]
    @return returnlist: [[["a","d"],["b","e],["c","f"]],[["g","j"],["h","k"],["i","l"]],...]
    '''
    returnlist = []
    for Pair in xrange(len(inputlist)):
        dummy2 = []
        for item in xrange(len(inputlist[Pair][0])):
            dummy = [Pair[0][item], Pair[1][item]]
            dummy2.append(dummy)
        returnlist.append(dummy2)

    return returnlist

编辑:退货清单中的对必须是清单。

提前致谢!!!

4

1 回答 1

4

看起来像一份工作zip()

>>> l = [[["a","b","c"],["d","e","f"]],[["g","h","i"],["j","k","l"]]]
>>> [zip(*item) for item in l]
[[('a', 'd'), ('b', 'e'), ('c', 'f')], [('g', 'j'), ('h', 'k'), ('i', 'l')]]

因此,您的功能将是:

def MakePairs(inputlist):
    return [zip(*item) for item in inputlist]

此外,请考虑使用itertools.izip()而不是 zip()。

于 2013-09-04T22:24:59.023 回答