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

lad*_*bug 1 python replace list

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

\n\n

我尝试使用isalpha()

\n\n
data = [\n    ['we', '\\n\\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best'], \n    ['though', '10/3/2011', 'it', 'may', 'not', '\\n\\n', 'make', 'landfall', 'all', 'week', '2 000 \xe2\x82\xac', 'if', 'it', 'follows', 'that', '\xe2\x80\xa2', 'track'],\n    ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '\xe2\x80\xa2', 'floods', '\\n\\n', 'are', 'possible'],\n]\n\nnew_data = ''.join([i for i in data if i.isalpha()])\n
Run Code Online (Sandbox Code Playgroud)\n\n

预期输出:

\n\n
['we will pray and hope for the best', \n 'though it may not make landfall all week if it follows that track',\n 'heavy rains capable of producing life threatening flash floods are possible']\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的输出:

\n\n
AttributeError: 'list' object has no attribute 'isalpha'\n
Run Code Online (Sandbox Code Playgroud)\n

MrG*_*eek 5

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

\n\n
data = [['we', '\\n\\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best'], \n        ['though', '10/3/2011', 'it', 'may', 'not', '\\n\\n', 'make', 'landfall', 'all', 'week', '2 000 \xe2\x82\xac', 'if', 'it', 'follows', 'that', '\xe2\x80\xa2', 'track'],\n        ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '\xe2\x80\xa2', 'floods', '\\n\\n', 'are', 'possible']]\n\nnew_data = [' '.join(i for i in sublist if all(j.isalpha() or j == ' ' for j in i)) for sublist in data]\n\nprint(new_data)\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
['we will pray and hope for the best',\n 'though it may not make landfall all week if it follows that track',\n 'heavy rains capable of producing life threatening flash floods are possible']\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如 @RonaldAaronson 向我指出的那样,如果您想从每个字符串中过滤掉非字母数字(+空格)字符,并且不完全忽略其中包含一些坏字符的字符串,您可以使用以下代码

\n\n
data = [['we', '\\n\\n', 'will', 'pray', 'and', 'hope', 'for', 'the', 'best.'], \n        ['though', '10/3/2011', 'it', 'may', 'not', '\\n\\n', 'make', 'landfall', 'all', 'week', '2 000 \xe2\x82\xac', 'if', 'it', 'follows', 'that', '\xe2\x80\xa2', 'track?'],\n        ['heavy', 'rains', 'capable', 'of', 'producing', 'life threatening', 'flash', '\xe2\x80\xa2', 'floods', '\\n\\n', 'are', 'possible!']]\n\nnew_data = [\n  ' '.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\n]\n\nprint(new_data)\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
['we will pray and hope for the best',\n 'though it may not make landfall all week if it follows that track',\n 'heavy rains capable of producing life threatening flash floods are possible']\n
Run Code Online (Sandbox Code Playgroud)\n