簡體   English   中英

返回由定界符分隔的字符串列表

[英]returning a list of strings separated by a delimiter

我在嘗試解決這個問題時遇到了一些問題。 它來自實踐考試,我似乎無法正確完成。 我應該寫一個python函數,該函數接受一個字符串和一個定界符,並返回一個列表,其中的字符串被除去定界符。 我們不允許使用split函數或“任何此類函數”。 我們在問題中收到的示例是這樣的

StringToken("this is so fun! I love it!", "!")

輸出

["this is so fun", "I love it"]

這是我編寫的代碼,它非常簡單。

def tokenizer(string, tmp):
    newStr = []
    for i in range(len(string)):
        if string[i] != tmp:
            newStr.append(string[i])
    return newStr

輸出是這個

['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 's', 'o', ' ', 'f', 'u', 'n', ' ', 'I', ' ', 'l', 'o', 'v', 'e', ' ', 'i', 't']

如何重新加入每個單詞?

如果將列表中的所有元素都加入,則會得到一個字符串,該字符串可能不是您想要的。

創建一個字符串,然后將其附加到列表中;

>>> def StringToken(string, tmp):
    newStrlist = []
    newStr = ''
    for i in range(len(string)):
        if string[i] != tmp:
            newStr += string[i]
        elif newStr != '':
            newStrlist.append(newStr)
            newStr = ''
    return newStrlist
... ... ... ... ... ... ... ... ... ... 
>>> StringToken("this is so fun! I love it!", "!")
['this is so fun', ' I love it']

有關說明,請參見代碼中的注釋。

def StringToken(string, tmp):
    newStr = ""   # A string to build upon
    lst = []      # The list to return
    for c in string: # Iterate over the characters
        if tmp == c: # Check for the character to strip
            if newStr != "":   # Prevent empty strings in output
                lst.append(newStr.strip())   # add to the output list
                newStr = ""                  # restart the string
                continue                     # move to the next character
        newStr += c  # Build the string
    return lst   # Return the list

產量

StringToken("this is so fun! I love it!", "!")
# ['this is so fun', 'I love it']

無需遍歷字符串中的所有字母,您可以使用find獲取下一個定界符的索引,然后相應地構建列表:

def tokenizer(string, delim):
    new_list = []
    while True:
        index = string.find(delim)  # use find to next occurrence of delimiter
        if index > -1:
            new_list.append(string[:index])
            string = string[index + len(delim):]
        else:
            new_list.append(string)
            break              # break because there is no delimiter present anymore

    # remove whitespaces and trim the existing strings 
    return [item.strip() for item in new_list if item.strip()]

用法:

>>> tokenizer("this is so fun! I love it!", "!")
["this is so fun", "I love it"]

這是一個比當前答案短一點的替代方法:

def StringToken(string, tmp):
    newStr = []
    start = 0
    for ind, char in enumerate(string):
        if char == tmp:
            newStr.append(string[start:ind])
            start = ind + 1
    return newStr

產量

>>> StringToken("this is so fun! I love it!", "!")
['this is so fun', ' I love it']

編輯:如果您想刪除示例中的前導或尾隨空格,則可以使用strip()完成:

def StringToken(string, tmp):
    newStr = []
    start = 0
    for ind, char in enumerate(string):
        if char == tmp:
            newStr.append(string[start:ind].strip())
            start = ind + 1
    return newStr

產量

>>> StringToken("this is so fun! I love it!", "!")
['this is so fun', 'I love it']

只需使用join運算符,這將使用給定的分隔符將整個列表連接在一起。 您可以在此處使用空定界符''。 嘗試:

a=['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 's', 'o', ' ', 'f', 'u', 'n', ' ', 'I', ' ', 'l', 'o', 'v', 'e', ' ', 'i', 't']
''.join(a)

輸出將是

'This is so fun I love it'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM