简体   繁体   中英

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('[%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):

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.

You should use the split() method. Taken from the official documentation:

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

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

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']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['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. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

For example, ' 1 2 3 '.split() returns ['1', '2', '3'], and ' 1 2 3 '.split(None, 1) returns ['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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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