在 Python 中,如何从列表中删除包含某些类型字符的任何元素?
抱歉,如果这是一个简单的问题,我对此还很陌生,但我花了一段时间寻找答案,但没有找到任何东西。我有一个看起来像这样可怕的混乱的列表:
['Organization name} ', '> (777) 777-7777} ', ' class="lsn-mB6 adr">1 Address, MA 02114 } ', ' class="lsn-serpListRadius lsn-fr">.2 Miles} MORE INFO YOUR LISTING MAP if (typeof(serps) !== \'undefined\') serps.arrArticleIds.push(\'4603114\'); ', 'Other organization} ', '> (555) 555-5555} ', ' class="lsn-mB6 adr">301 Address, MA 02121 } ', ' class="lsn-serpListRadius lsn-fr">.2 Miles} MORE INFO CLAIM YOUR LISTING MAP if (typeof(serps) !== \'undefined\') serps.arrArticleIds.push(\'4715945\'); ', 'Organization} ']
我需要处理它,以便 HTML.py 可以将里面的信息变成表格。由于某种原因,HTML.py 根本无法处理怪物元素(例如 'class="lsn-serpListRadius lsn-fr">.2 Miles} 更多信息您的列表地图 if (typeof(serps) !== \ '未定义\')serps.arrArticleIds.push(\'4603114\')'等)。对我来说幸运的是,我实际上并不关心怪物元素中的信息,而是想摆脱它们。
我尝试编写一个正则表达式来匹配所有两个以上字母的全大写单词,以识别怪物元素,并得到了这个:
re.compile('[^a-z]*[A-Z][^a-z]*\w{3,}')
但我不知道如何将其应用于删除包含与该正则表达式匹配的元素从列表中。我该怎么做/这是正确的方法吗?
Apologies if this is a simple question, I'm still pretty new to this, but I've spent a while looking for an answer and haven't found anything. I have a list that looks something like this horrifying mess:
['Organization name} ', '> (777) 777-7777} ', ' class="lsn-mB6 adr">1 Address, MA 02114 } ', ' class="lsn-serpListRadius lsn-fr">.2 Miles} MORE INFO YOUR LISTING MAP if (typeof(serps) !== \'undefined\') serps.arrArticleIds.push(\'4603114\'); ', 'Other organization} ', '> (555) 555-5555} ', ' class="lsn-mB6 adr">301 Address, MA 02121 } ', ' class="lsn-serpListRadius lsn-fr">.2 Miles} MORE INFO CLAIM YOUR LISTING MAP if (typeof(serps) !== \'undefined\') serps.arrArticleIds.push(\'4715945\'); ', 'Organization} ']
And I need to process it so that HTML.py can turn the information in it into a table. For some reason, HTML.py simply can't handle the monster elements (eg. 'class="lsn-serpListRadius lsn-fr">.2 Miles} MORE INFO YOUR LISTING MAP if (typeof(serps) !== \'undefined\') serps.arrArticleIds.push(\'4603114\'); ', etc). Fortunately for me, I don't actually care about the information in the monster elements and want to get rid of them.
I tried writing a regex that would match all more-than-two-letter all-caps words, to identify the monster elements, and got this:
re.compile('[^a-z]*[A-Z][^a-z]*\w{3,}')
But I don't know how to apply that to deleting the elements containing matches to that regex from the list. How would I do that/is that the right way to go about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有正则表达式
without regex
我认为您的正则表达式不正确,要匹配包含三个或更多字符的全大写单词的所有条目,您应该在
re.search
中使用类似的内容:这样您就可以使用列表理解进行过滤或
filter
内置函数:结果如下列表(我认为这就是您正在寻找的内容:
I think your regex is incorrect, to match all entries that contain all-cap words with three or more characters, you should use something like this with
re.search
:With that you can filter using a list comprehension or the
filter
built-in function:Results in the following list (which I think is what you are looking for:
首先,存储您的正则表达式,然后使用列表理解:
First, store your regex, then use a list comprehension:
或者完全相同但不编译正则表达式:
编辑:
Or the very same but without compiling regex:
Edited: