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

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

编辑:返回列表中的对必须是列表。
提前谢谢!!!

最佳答案

看起来像是一份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()。

关于python - 加快Python 2.7中的功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18624763/

10-11 16:02