简体   繁体   中英

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

Suppose, I want to split the following list into single character.

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

This is what I have tried so far:

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

Output should looks like:

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

But I am getting an error:

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

You can use re.findall(r'[^_ ]+', word) instead to split the underscore or space-delimited words. Also add another comprehension layer to flatten the possible tuples of strings:

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 would become:

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

You just mixed up what goes where.

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

There's an extra problem that ("horse") is not a tuple; ("horse",) is. Thus, ("horse") is merely "horse" in parentheses, and for animal in word will enumerate the individual letters in "horse" instead of giving you back one "horse" animal.

If you wish to split by other characters than _ , you can use re.split and a character class:

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

If you actually intended for the non-paired animals to not be tuples, then you will have to specially handle those cases:

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

Well here's a more readable code, Since I really don't like the idea of having an inline code no matter how efficient or faster it might be. Also, it might be easier for you to understand, and no library imports are required.

CODE:

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)

Output:

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

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