簡體   English   中英

在列表中,如何將每個字符串(帶有混合特殊字符)分隔為單個字符?

[英]In a list, how to separate each string (with mixed special characters) into a single character?

假設我想將以下列表拆分為單個字符。

mylist = [('dog', 'camel'), ('horse'), ('List_of_people_saved_by_Oskar'), 'mouse_bear', 'lion tiger rabbit', 'ant']

到目前為止,這是我嘗試過的:

L1 = [animal for word in mylist for animal in word.split('_')]
print(L1)

輸出應如下所示:

`['dog', 'camel', 'horse', 'List', 'of', 'people', 'saved', 'by', 'Oskar', 'mouse', 'bear', 'lion', 'tiger' 'rabbit', 'ant']`

但我收到一個錯誤:

AttributeError: 'tuple' object has no attribute 'split'

您可以使用re.findall(r'[^_ ]+', word)代替下划線或空格分隔的單詞。 還添加另一個理解層以平整可能的字符串元組:

import re
L1 = [animal for item in mylist for word in (item if isinstance(item, (tuple, list)) else (item,)) for animal in re.findall(r'[^_ ]+', word)]

L1將變為:

['dog', 'camel', 'horse', 'List', 'of', 'people', 'saved', 'by', 'Oskar', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant']

您只是混淆了去哪里。

[animal.split('_') for word in mylist for animal in word]

還有一個額外的問題是("horse")不是元組。 ("horse",)是。 因此, ("horse")在括號中僅是"horse"for animal in word將枚舉"horse"的各個字母,而不是退還一只"horse"動物。

如果希望除_以外的其他字符,可以使用re.split和一個字符類:

import re
[re.split(r'[_ ]', animal) for word in mylist for animal in word]

如果您實際上打算使非成對的動物成為元組,那么您將必須專門處理以下情況:

[re.split(r'[_ ]', animal)
    for word in mylist
    for animal in (word if isinstance(word, tuple) else (word,))]

好吧,這是一個更具可讀性的代碼,因為我實在不喜歡擁有內聯代碼的想法,無論它有多高效或更快。 同樣,這可能會使您更容易理解,並且不需要導入任何庫。

碼:

mylist = [('dog', 'camel'), ('horse'), ('List_of_people_saved_by_Oskar'), 'mouse_bear', 'lion tiger rabbit', 'ant']
new_list = []

for items in mylist:
    if type(items) == tuple:
        for animals in items:
            new_list.append(animals)
    elif '_' in items:
        new_animal = items.split('_')
        for animals in new_animal:
            new_list.append(animals)

    elif ',' in items:
        new_animal = items.split(',')
        for animals in new_animal:
            new_list.append(animals)

    elif ' ' in items:
        new_animal = items.split(' ')
        for animals in new_animal:
            new_list.append(animals)
    else:
        new_list.append(items)
print(new_list)

輸出:

['dog', 'camel', 'horse', 'List', 'of', 'people', 'saved', 'by', 'Oskar', 'mouse', 'bear', 'lion', 'tiger', 'rabbit', 'ant']

暫無
暫無

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

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