Is it possible to assign a default value when unpacking?

会有一股神秘感。 提交于 2019-12-12 07:47:21

问题


I have the following:

>>> myString = "has spaces"
>>> first, second = myString.split()
>>> myString = "doesNotHaveSpaces"
>>> first, second = myString.split()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

I would like to have second default to None if the string does not have any white space. I currently have the following, but am wondering if it can be done in one line:

splitted = myString.split(maxsplit=1)
first = splitted[0]
second = splitted[1:] or None

回答1:


May I suggest you to consider using a different method, i.e. partition instead of split:

>>> myString = "has spaces"
>>> left, separator, right = myString.partition(' ')
>>> left
'has'
>>> myString = "doesNotHaveSpaces"
>>> left, separator, right = myString.partition(' ')
>>> left
'doesNotHaveSpaces'

If you are on python3, you have this option available:

>>> myString = "doesNotHaveSpaces"
>>> first, *rest = myString.split()
>>> first
'doesNotHaveSpaces'
>>> rest
[]



回答2:


Here's one general solution to unpack tuple and use default value if tuple is shorter than expected:

unpacker = lambda x,y=1,z=2:(x,y,z)

packed = (8,5)
a,b,c = unpacker(*packed)
print(a,b,c) # 8 5 2

packed = (8,)
a,b,c = unpacker(*packed)
print(a,b,c) # 8 1 2

Play with this code




回答3:


A general solution would be to chain your iterable with a repeat of None values and then use an islice of the result:

from itertools import chain, islice, repeat

none_repat = repeat(None)
example_iter = iter(range(1)) #or range(2) or range(0)

first, second = islice(chain(example_iter, none_repeat), 2)

this would fill in missing values with None, if you need this kind of functionality a lot you can put it into a function like this:

def fill_iter(it, size, fill_value=None):
    return islice(chain(it, repeat(fill_value)), size)

Although the most common use is by far for strings which is why str.partition exists.




回答4:


You could try this:

NUM2UNPACK=2   
parts = myString.split()    
first, second = parts+[None]*(NUM2UNPACK-(len(parts)))     


来源:https://stackoverflow.com/questions/38666283/is-it-possible-to-assign-a-default-value-when-unpacking

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