Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串拆分为固定数量标记列表的简明方法_Python_Python 3.x - Fatal编程技术网

Python 将字符串拆分为固定数量标记列表的简明方法

Python 将字符串拆分为固定数量标记列表的简明方法,python,python-3.x,Python,Python 3.x,我正在编写一段代码,需要将以连字符分隔的字符串拆分为最多三个标记。如果拆分后的令牌少于三个,则应附加足够数量的空字符串以生成三个令牌 例如,“foo-bar-baz”应拆分为['foo','bar','baz',],但foo-bar应拆分为['foo','bar',] 这是我写的代码 def three_tokens(s): tokens = s.split('-', 2) if len(tokens) == 1: tokens.append('')

我正在编写一段代码,需要将以连字符分隔的字符串拆分为最多三个标记。如果拆分后的令牌少于三个,则应附加足够数量的空字符串以生成三个令牌

例如,“foo-bar-baz”应拆分为['foo','bar','baz',],但foo-bar应拆分为['foo','bar',]

这是我写的代码

def three_tokens(s):
    tokens = s.split('-', 2)
    if len(tokens) == 1:
        tokens.append('')
        tokens.append('')
    elif len(tokens) == 2:
        tokens.append('')
    return tokens

print(three_tokens(''))
print(three_tokens('foo'))
print(three_tokens('foo-bar'))
print(three_tokens('foo-bar-baz'))
print(three_tokens('foo-bar-baz-qux'))
以下是输出:

['', '', '']
['foo', '', '']
['foo', 'bar', '']
['foo', 'bar', 'baz']
['foo', 'bar', 'baz-qux']
我的问题是,对于这个小任务来说,我编写的three_tokens函数似乎过于冗长。是否有一种Python式的编写方法,或者是否有某种Python函数或类专门用于执行此类任务,从而使代码更加简洁?

您可以使用一个简单的while循环:

或者使用串联,以便将其放入return语句中:

def three_tokens(s):
    tokens = s.split('-', 2)
    return tokens + [''] * (3 - len(tokens))
您可以使用一个简单的while循环:

或者使用串联,以便将其放入return语句中:

def three_tokens(s):
    tokens = s.split('-', 2)
    return tokens + [''] * (3 - len(tokens))
使用:

使用:

这可能行得通

tokens = s.split('-', 2)
tokens += [''] * max(0, 3 - len(tokens))
这可能行得通

tokens = s.split('-', 2)
tokens += [''] * max(0, 3 - len(tokens))

这可能有些过分,但您可以使用itertools中的一些方法


这可能有些过分,但您可以使用itertools中的一些方法

…收益率

>>> three_tokens('foo')
['foo', '', '']

>>> three_tokens('foo-bar')
['foo', 'bar', '']

>>> three_tokens('foo-bar-baz')
['foo', 'bar', 'baz']

>>> three_tokens('foo-bar-baz-buzz')
['foo', 'bar', 'baz-buzz']
…收益率

>>> three_tokens('foo')
['foo', '', '']

>>> three_tokens('foo-bar')
['foo', 'bar', '']

>>> three_tokens('foo-bar-baz')
['foo', 'bar', 'baz']

>>> three_tokens('foo-bar-baz-buzz')
['foo', 'bar', 'baz-buzz']
这个怎么样

def three_tokens(s):
    output = ['', '', '']
    tokens = s.split('-', 2)
    output[0:len(tokens)] = tokens
    return output
还有一个oneliner:

three_tokens = lambda s: (s.split('-', 2) + ['', ''])[:3]
顺便说一句,我觉得你的解决方案没有任何不符合Python的地方。这有点冗长,但意图非常明确

还有一点:

def three_tokens(s):
   it = iter(s.split('-', 2))
   return [ next(it, '') for _ in range(3) ]
这个怎么样

def three_tokens(s):
    output = ['', '', '']
    tokens = s.split('-', 2)
    output[0:len(tokens)] = tokens
    return output
还有一个oneliner:

three_tokens = lambda s: (s.split('-', 2) + ['', ''])[:3]
顺便说一句,我觉得你的解决方案没有任何不符合Python的地方。这有点冗长,但意图非常明确

还有一点:

def three_tokens(s):
   it = iter(s.split('-', 2))
   return [ next(it, '') for _ in range(3) ]

不需要最大值;对s.split的限制确保在任何情况下令牌中的元素都不会超过3个。此外,将列表乘以负数也会导致空列表;max在这里完全是过火了。不需要max;对s.split的限制确保在任何情况下令牌中的元素都不会超过3个。此外,将列表乘以负数也会导致空列表;这里的max完全是多余的。为什么使用if语句和b[:]副本?[]*0是一个空列表,按片复制的列表是完全冗余的,并且浪费了周期。是的。b[:]不是必需的。b+[]*max1,2-lenb应该可以做到。看我的,你不需要max。为什么使用if语句和b[:]副本?[]*0是一个空列表,按片复制的列表是完全冗余的,并且浪费了周期。是的。b[:]不是必需的。b+[]*max1,2-lenb应该可以。看我的,你不需要max。