我想知道,如果有办法加快这里显示的功能。我知道这看起来不是非常pythonic ...
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
编辑:返回列表中的对必须是列表。
提前致谢!!!
答案 0 :(得分: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()。