Idiomatic way to unpack variable length list of maximum size n

荒凉一梦 提交于 2019-11-28 03:06:57

问题


I'm reading a file and unpacking each line like this:

for line in filter(fh):
  a, b, c, d = line.split()

However, it's possible that line may have more or fewer columns than the variables I wish to unpack. In the case when there are fewer, I'd like to assign None to the dangling variables, and in the case where there are more, I'd like to ignore them. What's the idiomatic way to do this? I'm using python 2.7.


回答1:


Fix the length of the list, padding with None.

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



回答2:


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.




回答3:


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




回答4:


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)


来源:https://stackoverflow.com/questions/20501440/idiomatic-way-to-unpack-variable-length-list-of-maximum-size-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!