"""unzip simple list-like obj
"""
def unzip(p, n):
""" Example:
>>> unzip(['a','b','c','d','e'], 3)
[('a', 'd'), ('b', 'e'), ('c', None)]
"""
mlen, lft = divmod(len(p), n)
if lft != 0: mlen += 1
lst = [[None]*mlen for i in range(n)]
for i in range(len(p)):
j, k = divmod(i, n)
lst[k][j] = p[i]
return map(tuple, lst)
print unzip(['a','b','c','d','e'], 3)
##########################################
the output is:
>>>
[('a', 'd'), ('b', 'e'), ('c', None)]
>>>
431



被折叠的 条评论
为什么被折叠?



