我有以下内容:
>>> 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
如果字符串没有任何空格,我希望将
second
默认为 None
。我目前有以下内容,但我想知道是否可以在一行中完成:
splitted = myString.split(maxsplit=1)
first = splitted[0]
second = splitted[1:] or None
我建议您考虑使用不同的方法,即
partition
而不是split
:
>>> myString = "has spaces"
>>> left, separator, right = myString.partition(' ')
>>> left
'has'
>>> myString = "doesNotHaveSpaces"
>>> left, separator, right = myString.partition(' ')
>>> left
'doesNotHaveSpaces'
如果您使用的是 python3,则可以使用此选项:
>>> myString = "doesNotHaveSpaces"
>>> first, *rest = myString.split()
>>> first
'doesNotHaveSpaces'
>>> rest
[]
chain
的 repeat
值对
None
进行迭代,然后使用结果的 islice
:
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)
这将用
None
填充缺失值,如果你经常需要这种功能,你可以将它放入这样的函数中:
def fill_iter(it, size, fill_value=None):
return islice(chain(it, repeat(fill_value)), size)
str.partition
存在的原因。
这是解压元组并在元组短于预期时使用默认值的一种通用解决方案:
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
这里肯定有一些......呃有趣的答案。
def fixed_width_split(s: str, delim: str, n: int, fill: Any = None):
parts = s.split(delim)
return parts[:n] + [fill] * (n - len(parts))
让我们尝试一下:
>>> fixed_width_split("1:2:3", ":", 4)
['1', '2', '3', None]
>>> fixed_width_split("1:2:3", ":", 2)
['1', '2']
>>> fixed_width_split("1:2:3", ":", 5, fill=0)
['1', '2', '3', 0, 0]
所以你可以在需要解包成固定数量的值并保证能够解包的情况下使用它。
a, b, c = fixed_width_split(mystr, ":", 3)
关于有点迟钝的语法的一些细节。
# take at most `n` elements from the original split.
parts[:n]
# make an array of x elements with value fill
[fill] * x
[1] * 3 == [1, 1, 1]
# negative values are equivalent to 0.
[1] * -2 == []
# find the difference between the target length and the length of the original split.
n - len(parts)
你可以试试这个:
NUM2UNPACK=2
parts = myString.split()
first, second = parts+[None]*(NUM2UNPACK-(len(parts)))