暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

python 解析zip()函数和zip_longest()函数

心在远方AND走在路上 2021-09-01
771

python 解析zip函数和itertools.zip_longest函数

一、zip:用来使列表一一对应,该函数返回一个以元组为元素的列表。返回的列表长度为最短列表的长度


1、当只有一个参数时:

    yc = ['a', 'b', 'c', 'd']
    dd = list(zip(yc))
    print(dd)

    输出结果:

      [('a',), ('b',), ('c',), ('d',)]


      2、当有两个参数时:

        yc = ['a', 'b', 'c', 'd']
        num = [1, 2, 3]
        dd = list(zip(yc, num))
        print(dd)


        输出结果:

          [('a', 1), ('b', 2), ('c', 3)]


          3、当有多个参数时:


            yc = ['a', 'b', 'c', 'd']
            num = [1, 2, 3]
            name = ['yangchao''xiaman']
            dd = list(zip(yc, num, name))
            print(dd)

            输出结果:

              [('a', 1, 'yangchao'), ('b', 2, 'xiaman')]


              二、zip_longest具体可以用来对列表的一一对应,如果列表的长度不一致,则其会选择最长的那个列表,并将没有的填充为None(这个可以自己传参时设置)


              1、当只有一个参数时:

                import itertools
                yc = ['a', 'b', 'c', 'd']
                num = [1, 2, 3]
                name = ['yangchao', 'xiaman']
                dic =[ll for ll in itertools.zip_longest(yc)]
                print(dic)

                输出结果:

                  [('a',), ('b',), ('c',), ('d',)]

                  2、当有两个参数时:

                    import itertools
                    yc = ['a', 'b', 'c', 'd']
                    num = [1, 2, 3]
                    dic1 =[ll for ll in itertools.zip_longest(yc, num)]
                    print(dic1)
                    dic2 ={ll:nn for ll,nn in itertools.zip_longest(yc, num)}##这里可以对指定元组中的每一个元素进行操作
                    print(dic2)

                    输出结果:

                      [('a', 1), ('b', 2), ('c', 3), ('d', None)]
                      {'a': 1, 'b': 2, 'c': 3, 'd': None}


                      3、当有多个参数时:

                        import itertools
                        yc = ['a', 'b', 'c', 'd']
                        num = [1, 2, 3]
                        name = ['yangchao', 'xiaman']
                        dic =[ll for ll in itertools.zip_longest(yc, num, name)]
                        print(dic)

                        输出结果:

                          [('a', 1, 'yangchao'), ('b', 2, 'xiaman'), ('c', 3, None), ('d', None, None)]


                          三、最后 附上itertools.zip_longest源码:

                            def itertools.zip_longest(*args, fillvalue=None):
                            # itertools.zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
                            iterators = [iter(it) for it in args]
                            num_active = len(iterators)
                            if not num_active:
                            return
                            while True:
                            values = []
                            for i, it in enumerate(iterators):
                            try:
                            value = next(it)
                            except StopIteration:
                            num_active -= 1
                            if not num_active:
                            return
                            iterators[i] = repeat(fillvalue)
                            value = fillvalue
                            values.append(value)
                            yield tuple(values)




                            文章转载自心在远方AND走在路上,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                            评论