zip函数编写--python学习笔记30

简介: zip函数编写--python学习笔记30

实现一个函数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

20170108001758399.png


源代码有问题,特贴出新的代码:

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)
目录
相关文章
|
4天前
|
Python
python函数进阶
python函数进阶
|
4天前
|
安全 Python
Python量化炒股的获取数据函数—get_industry()
Python量化炒股的获取数据函数—get_industry()
11 3
|
4天前
|
Python
Python sorted() 函数和sort()函数对比分析
Python sorted() 函数和sort()函数对比分析
|
4天前
|
Python
Python量化炒股的获取数据函数—get_security_info()
Python量化炒股的获取数据函数—get_security_info()
10 1
|
4天前
|
Python
Python量化炒股的获取数据函数— get_billboard_list()
Python量化炒股的获取数据函数— get_billboard_list()
|
4天前
|
安全 数据库 数据格式
Python量化炒股的获取数据函数—get_fundamentals()
Python量化炒股的获取数据函数—get_fundamentals()
10 0
|
4天前
|
算法 Python
Python编程的函数—内置函数
Python编程的函数—内置函数
|
存储 Linux 索引
python基础学习笔记
服务器 1.ftp服务器         FTP是FileTransferProtocol(文件传输协议)的英文简称,中文名称为“文传协议”。
1482 0
|
数据安全/隐私保护 Python
下一篇
无影云桌面