实现一个函数zip2,其功能同python內建函数zip,但返回是不是列表对象而是生成器;
zip方法功能说明:
l = [1,2,3,4] ll = [5,6,7,8] lll = [8,9,10,11] zip(l, ll) --> [(1, 5), (2, 6), (3, 7), (4, 8)] zip(l,ll,lll) --> [(1, 5, 8), (2, 6, 9), (3, 7, 10), (4, 8, 11)] zip(l, ll, lll, ...) --> [(1, 5, 8, ...), (2, 6, 9, ...), (3, 7, 10, ...), (4, 8, 11, ...)] ############################################################## #################################################### def zip2(*seq): s=[] import numpy as np s1=np.array(seq) for i in range(len(s1)): s.append(tuple(s1[i])) return s a = [1,2,3,4] b = [5,6,7,8] c = [8,9,10,11] d=[12,13,14,15] z=zip2(a,b,c,d) z
源代码有问题,特贴出新的代码:
a = [1,2,3,4] b = [5,6,7,8] c = [8,9,10,11] d=[12,13,14,15] class MyList(list): def append(self, value): super(MyList, self).append(value) return self def zip2(*seq): s=list(seq) print(s) news=MyList() for j in range(len(s[0])): a=MyList() for i in range(len(s)): v=a.append(s[i][j]) news=news.append(v) return news zip2(a,b,c,d)