Idiomatic way to unpack variable length list of maximum size n

为君一笑 提交于 2019-11-29 09:55:10

Fix the length of the list, padding with None.

def fixLength(lst, length):
    return (lst + [None] * length)[:length]

First of all, think about why you want to do this.

However, given that you want to (1) pad with None and (2) ignore extra variables, the code is easy:

a,b,c,d = (line.split() + [None]*4)[:4]

Obviously, the magic number has to be the same as the number of variables. This will extend what you have with the magic number, then trim back down to that length.

For an arbitrary iterable you can do:

import itertools

def padslice(seq,n):
    return itertools.islice(itertools.chain(seq,itertools.repeat(None)), n)

This is the same pad-and-slice with itertools.

In python 3 you can use this

a, b, c, d, *_unused_ = line.split() + [None]*4

Edit

For large strings I suggest to use maxsplit-argument for split (this argument also works in py2.7):

a, b, c, d, *_unused_ = line.split(None, 4) + [None]*4

Why 5? Otherwise the 4th element would consist the whole residual of the line.

Edit2 It is 4… It stops after 4 splits, not 4 elements

Something like this, works for any iterable/iterator. If you're always going to pass a list then you can remove the islice part.

from itertools import islice
def solve(seq, n):
    lis = list(islice(seq, n))
    return lis + [None]*(n - len(lis))
... 
>>> a, b, c, d = solve(range(2), 4)
>>> a, b, c, d
(0, 1, None, None)
>>> a, b, c, d = solve('qwe', 4)
>>> a, b, c, d
('q', 'w', 'e', None)
>>> a, b, c, d = solve(iter([1, 2, 3, 4, 5]), 4)
>>> a, b, c, d
(1, 2, 3, 4)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!