【问题标题】:In a list, how to separate each string (with mixed special characters) into a single character?在列表中,如何将每个字符串(混合特殊字符)分成一个字符?
【发布时间】:2019-03-20 16:43:07
【问题描述】:

假设,我想将以下列表拆分为单个字符。

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'

【问题讨论】:

  • 什么算作特殊字符
  • 对不起,我没有找到你
  • 除了空格(' ')和下划线('_')还有什么?
  • 只有SpaceUnderscore 存在。除此之外,该列表可能还有成对的字符串,例如('dog', 'camel')
  • 您希望代码特定于给定的输入或任何它的方式?另外,输入的内容可以与您已经提供的不同吗?

标签: python string python-3.x list nltk


【解决方案1】:

您可以改用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']

【讨论】:

  • 谢谢@blhsing。 re.findall(r'[^_ ]+', word) 是关于什么的?
  • re.findall 在列表中查找与给定正则表达式匹配的所有子字符串,这里的正则表达式模式匹配既不是_也不是`. Using re.findall`的字符序列更安全而不是使用re.split,因为如果你使用re.split(r'[_ ]', word),那么当输入以分隔符开始或结束或有双下划线或双空格时,你可能会得到空字符串。
  • 如果您也想将点作为分隔符,只需将其添加到正则表达式模式中,您就可以改为使用re.findall(r'[^_ .]+', word)
  • 是的,没错。如果您要添加更多分隔符,请始终将破折号保留在字符类的前面或最后,就像您现在正在做的那样,这样它就不会被解释为[a-z]中的字符范围。
  • 非常感谢@blhsing
【解决方案2】:

你只是混淆了去哪里。

[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,))]

【讨论】:

  • 这似乎不起作用。 '_' 不是唯一的分隔符。
  • @Ev.Kounis 不幸的是,我只能通过问题中实际写的内容。有代码;我解决了。
  • 如果 OP 的混乱引导了本网站的答案,那么它就没有多大用处了(恕我直言)。
  • 谢谢@Amadan 它有帮助!
【解决方案3】:

这里有一个更易读的代码,因为我真的不喜欢内联代码的想法,无论它可能多么高效或更快。此外,它可能更容易理解,并且不需要库导入。

代码:

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']

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 2020-01-15
    • 1970-01-01
    • 2012-02-14
    • 2013-12-25
    相关资源
    最近更新 更多