Some built-in to pad a list in python

前端 未结 10 1986
暗喜
暗喜 2020-11-27 14:06

I have a list of size < N and I want to pad it up to the size N with a value.

Certainly, I can use something like the following, but I feel that there sh

10条回答
  •  不知归路
    2020-11-27 14:38

    If you want to pad with None instead of '', map() does the job:

    >>> map(None,[1,2,3],xrange(7))
    
    [(1, 0), (2, 1), (3, 2), (None, 3), (None, 4), (None, 5), (None, 6)]
    
    >>> zip(*map(None,[1,2,3],xrange(7)))[0]
    
    (1, 2, 3, None, None, None, None)
    

提交回复
热议问题