[英]Remove '\n\n\n', '\n' from python list
How can I remove '\\n\\n\\n'
, '\\n'
, u'\\xa0'
from the array below? 如何从下面的数组中删除
'\\n\\n\\n'
, '\\n'
, u'\\xa0'
?
list = ['\n\n\n', '\n', '1', '2', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', u'\xa0', '\n', u'\xa0\xa0', '\n', '3']
I have tried .remove("'\\n'")
but I get the error message: list.remove(x): x not in list 我尝试过
.remove("'\\n'")
但收到错误消息:list.remove(x):x不在列表中
使用数组理解:
[x for x in list if not x.isspace() and u'\\xa0' not in x]
Instead of targeting the non-alphanumeric characters, search for the alphanumeric characters themselves: 而不是定位非字母数字字符,而是搜索字母数字字符本身:
import re
l = ['\n\n\n', '\n', '1', '2', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', u'\xa0', '\n', u'\xa0\xa0', '\n', '3']
new_l = filter(lambda x:re.findall('^[a-zA-Z0-9]+$', x), l)
Output: 输出:
['1', '2', '3']
I wouldnt name the list list
better something like: 我不会命名名单
list
更好的是这样的:
l = ['\n\n\n', '\n', '1', '2', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n', u'\xa0', '\n', u'\xa0\xa0', '\n', '3']
Then you can try filter
: 然后,您可以尝试
filter
:
> list(filter(lambda x: '\n' not in x and u'\xa0' not in x, l))
> ['1', '2', '3']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.