在Python中,如何从列表中删除任何包含特定类型字符的元素?

2024-03-28 13:03:31 发布

您现在位置: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根本无法处理monster元素(例如,'class=“lsn serplistadius lsn fr”>;.2 Miles}如果(typeof(serps))更多信息,请查看列表地图!==\'未定义')serps.arrArticleIds.push(\'4603114');'等)。幸运的是,我并不关心怪物元素中的信息,我想把它们去掉。

我试着写一个正则表达式来匹配所有超过两个字母的大写单词,以识别怪物元素,得到了这个:

re.compile('[^a-z]*[A-Z][^a-z]*\w{3,}')

但我不知道如何将其应用于从列表中删除包含与该正则表达式匹配的元素。我该怎么做/这样做对吗?


Tags: 信息元素addressfrpushclassadrorganization
3条回答

我认为您的regex是不正确的,要匹配包含三个或三个以上字符的所有大写单词的所有条目,您应该在re.search中使用类似的内容:

regex = re.compile(r'\b[A-Z]{3,}\b')

使用它,您可以使用列表理解或内置函数filter进行筛选:

full = ['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} ']
regex = re.compile(r'\b[A-Z]{3,}\b')
# use only one of the following lines, whichever you prefer
filtered = filter(lambda i: not regex.search(i), full)
filtered = [i for i in full if not regex.search(i)]

结果如下所示(我认为这正是您要寻找的:

>>> pprint.pprint(filtered)
['Organization name} ',
 '> (777) 777-7777} ',
 ' class="lsn-mB6 adr">1 Address, MA 02114 } ',
 'Other organization} ',
 '> (555) 555-5555} ',
 ' class="lsn-mB6 adr">301 Address, MA 02121 } ',
 'Organization} ']

首先,存储正则表达式,然后使用列表理解:

regex = re.compile('[^a-z]*[A-Z][^a-z]*\w{3,}')
okay_items = [x for x in all_items if not regex.match(x)]

或者完全相同但不编译regex:

from re import match

ll = ['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} ']

filteredData = [x for x in ll if not match(r'[^a-z]*[A-Z][^a-z]*\w{3,}', x)]

编辑:

from re import compile

rex = compile('[^a-z]*[A-Z][^a-z]*\w{3,}')
filteredData = [x for x in ll if not rex.match(x)]

相关问题 更多 >