简体   繁体   English

如何简化此功能?

[英]How can I simplify this function?

Is there any way to simplify this function? 有什么方法可以简化此功能? Specifically, I'd like to rewrite it with fewer lines of indentation. 具体来说,我想用更少的缩进行来重写它。

# split string (first argument) at location of separators (second argument, should be a string)
def split_string(text, separators):
    text = ' ' + text + ' '
    words = []
    word = ""
    for char in text:
        if char not in separators:
            word += char
        else:
            if word:
                words.append(word)
            word = ""
    if not words:
        words.append(text)
    return words

Try using re.split , for example: 尝试使用re.split ,例如:

re.split('[%s]' % (separators),string)

The [] creates a regular expression character class to split on. []创建正则表达式字符类进行拆分。

Your code seems to produce 您的代码似乎产生了

>>> split_string("foo.,.bar", ".,")
[' foo']

but your comment says 但是你的评论说

split_string("foo.,.bar", ".,") will return ["foo", "bar"]

Assuming the comment is what's intended, then I'd use itertools.groupby (I hate using regexes): 假设注释是什么意思,那么我将使用itertools.groupby (我讨厌使用正则表达式):

from itertools import groupby

def splitter(text, separators):
    grouped = groupby(text, lambda c: c in separators)
    return [''.join(g) for k,g in grouped if not k]

which gives 这使

>>> splitter("foo.,.bar", ".,")
['foo', 'bar']

groupby returns an iterator over consecutive terms grouped by some function -- in this case, lambda c: c in separators -- of the terms. groupby返回按术语的某些函数(在本例中为lambda c: c in separators分组的连续术语上的迭代器。

You should use the split() method. 您应该使用split()方法。 Taken from the official documentation: 取自官方文件:

str.split([sep[, maxsplit]]) str.split([sep [,maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. 使用sep作为分隔符字符串,返回字符串中单词的列表。 If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). 如果指定了maxsplit,则最多完成maxsplit个分割(因此,列表最多包含maxsplit + 1个元素)。 If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made). 如果未指定maxsplit或-1,则分割数没有限制(进行所有可能的分割)。

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). 如果给定sep,则不将连续的定界符分组在一起,而是将其视为定界空字符串(例如'1,,2'.split(',')返回['1','','2'])。 The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). sep参数可以包含多个字符(例如,'1 <> 2 <> 3'.split('<>')返回['1','2','3'])。 Splitting an empty string with a specified separator returns ['']. 使用指定的分隔符分割空字符串将返回['']。

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. 如果未指定sep或为None,则将应用不同的拆分算法:连续的空白行将被视为单个分隔符,并且如果字符串的开头或结尾处有空格,则结果在开头或结尾将不包含空字符串。 Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns []. 因此,使用None分隔符拆分空字符串或仅包含空格的字符串将返回[]。

For example, ' 1 2 3 '.split() returns ['1', '2', '3'], and ' 1 2 3 '.split(None, 1) returns ['1', '2 3 ']. 例如,'1 2 3'.split()返回['1','2','3'],'1 2 3'.split(None,1)返回['1','2 3' ]。

You can do: 你可以做:

myString = "Some-text-here"
splitWords = myString.split("-")

The above code will return a List of with the words separated. 上面的代码将返回一个列表,其中包含单词。 I used "-" as the delimiter, you may assign any delimiter you like. 我使用“-”作为分隔符,您可以分配任何喜欢的分隔符。 Default is the "space" delimiter like this: 默认为“空格”定界符,如下所示:

myString = "Some text here"
splitWords = myString.split()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM