【问题标题】:Iterate over nested list and operate on each element [duplicate]迭代嵌套列表并对每个元素进行操作[重复]
【发布时间】:2020-01-05 01:36:19
【问题描述】:

我有一个列表列表,我正在尝试删除所有非字母字符。

我尝试使用isalpha()

data = [
    ['we', '\n\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best'], 
    ['though', '10/3/2011', 'it', 'may', 'not', '\n\n', 'make', 'landfall', 'all', 'week', '2 000 €', 'if', 'it', 'follows', 'that', '•', 'track'],
    ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '•', 'floods', '\n\n', 'are', 'possible'],
]

new_data = ''.join([i for i in data if i.isalpha()])

预期输出:

['we will pray and hope for the best', 
 'though it may not make landfall all week if it follows that track',
 'heavy rains capable of producing life threatening flash floods are possible']

我的输出:

AttributeError: 'list' object has no attribute 'isalpha'

【问题讨论】:

    标签: python list replace


    【解决方案1】:

    由于您有嵌套列表(以及其中的字符串),因此您需要为此使用嵌套列表推导(子列表的最外层、字符串的内层和字符的最内层)和 @987654321 @ 与' ' 得到你想要的结果:

    data = [['we', '\n\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best'], 
            ['though', '10/3/2011', 'it', 'may', 'not', '\n\n', 'make', 'landfall', 'all', 'week', '2 000 €', 'if', 'it', 'follows', 'that', '•', 'track'],
            ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '•', 'floods', '\n\n', 'are', 'possible']]
    
    new_data = [' '.join(i for i in sublist if all(j.isalpha() or j == ' ' for j in i)) for sublist in data]
    
    print(new_data)
    

    输出:

    ['we will pray and hope for the best',
     'though it may not make landfall all week if it follows that track',
     'heavy rains capable of producing life threatening flash floods are possible']
    

    正如@RonaldAaronson 向我指出的那样,如果您想从每个字符串中过滤掉非字母数字(+空格)字符,而不是完全忽略某些的字符串其中的字符,您可以使用它来代替:

    data = [['we', '\n\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best.'], 
            ['though', '10/3/2011', 'it', 'may', 'not', '\n\n', 'make', 'landfall', 'all', 'week', '2 000 €', 'if', 'it', 'follows', 'that', '•', 'track?'],
            ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '•', 'floods', '\n\n', 'are', 'possible!']]
    
    new_data = [
      ' '.join(x.strip() for x in (''.join(c for c in s if c.isalpha() or c == ' ') for s in sl) if x) for sl in data
    ]
    
    print(new_data)
    

    输出:

    ['we will pray and hope for the best',
     'though it may not make landfall all week if it follows that track',
     'heavy rains capable of producing life threatening flash floods are possible']
    

    【讨论】:

    • 因此,如果在第一个列表中'best' 'best.',您似乎会删除整个字符串。这真的是我们想要的结果吗?
    • @RonaldAaronson 不错的想法,我想这证明了预期结果的模棱两可,因为 OP 没有给出这样的例子,但我会包括在内。
    • OP 说,“删除所有非字母字符”而不是“删除所有包含至少一个非字母字符的字符串”,但随后无可否认地继续提供了一个不太好的例子肯定会令人困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2015-12-25
    • 1970-01-01
    • 2012-03-22
    相关资源
    最近更新 更多