Outerzip / zip longest function (with multiple fill values)

前端 未结 3 1951
有刺的猬
有刺的猬 2021-01-11 17:19

Is there a Python function an \"outer-zip\", which is a extension of zip with different default values for each iterable?

a = [1, 2, 3]   # associate a defa         


        
3条回答
  •  甜味超标
    2021-01-11 17:59

    This function can be defined by extending each inputted list and zipping:

    def outerzip(*args):
        # args = (a, default_a), (b, default_b), ...
        max_length = max( map( lambda s: len(s[0]), args))
        extended_args = [ s[0] + [s[1]]*(max_length-len(s[0])) for s in args ]
        return zip(*extended_args)
    
    outerzip((a, 0), (b, 1)) # [(1, 4), (2, 5), (3, 6), (0, 7)]
    

提交回复
热议问题