Python 迭代嵌套列表并对每个元素进行操作

Python 迭代嵌套列表并对每个元素进行操作,python,list,replace,Python,List,Replace,我有一个列表,我正在尝试删除所有非字母字符 我尝试使用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'] 我的输出: AttributeE

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

我尝试使用
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'

由于您有嵌套列表(以及其中的字符串),因此需要为此使用嵌套列表理解(最外层用于子列表,最内层用于字符串,最内层用于字符),并使用
'
连接
,以获得所需的结果:

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

由于您有嵌套列表(以及其中的字符串),因此需要为此使用嵌套列表理解(最外层用于子列表,最内层用于字符串,最内层用于字符),并使用
'
连接
,以获得所需的结果:

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']
['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']
['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.
,那么您将删除整个字符串。这真的是期望的结果吗?@Ronaldaronson Nice认为,我想这证明了期望结果的模糊性,因为OP没有给出这样的例子,但我将包括这一点。OP说,“删除所有非字母字符”不是“删除所有包含至少一个非字母字符的字符串”但无可否认,接下来提供了一个不太好的例子,这肯定会让人困惑。因此,如果第一个列表中的
'best'
'best.
,那么您似乎会删除整个字符串。这真的是期望的结果吗?@Ronaldaronson Nice认为,我想这证明了期望结果的模糊性,因为OP没有给出这样的例子,但我将包括这一点。OP说,“删除所有非字母字符”不是“删除所有包含至少一个非字母字符的字符串”但无可否认的是,接下来提供了一个不太好的例子,肯定会让人困惑。