查找列表项并用列表项替换列表项

2024-05-16 23:01:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我的清单如下:

c11=[' YandexBot/3.0, +http://yandex.com/bots', ' Win64, x64', ' AhrefsBot/5.2, +http://ahrefs.com/robot/', ' Android 7.1.2, Redmi 4 Build/N2G47H', ' Android 7.0, EVA-L09 Build/HUAWEIEVA-L09', ' Android 6.0.1, Redmi Note 4 Build/MMB29M', ' Googlebot/2.1, +http://www.google.com/bot.html', ' Android 6.0.1, CPH1701 Build/MMB29M', ' Android 6.0.1, Redmi 4 Build/MMB29M', ' Android 6.0.1, SM-J500F Build/MMB29M', ' uCrawler/1.0 , +https://blog.ucoz.ru/upolicy', ' SurdotlyBot/1.0, +http://sur.ly/bot.html', ' Opera Mini/8.0.40377/85.73, U', ' Pinterestbot/1.0, +http://www.pinterest.com/bot.html', ' Android 7.0, SM-J701F Build/NRD90M', ' Android 6.0.1, Nexus 5X Build/MMB29P', ' Android 6.0.1, SM-A500H Build/MMB29M', ' Android 7.1.1, SM-T385 Build/NMF26X', ' SemrushBot/1.2~bl, +http://www.semrush.com/bot.html', ' Android 6.0.1, ONE A2003 Build/MMB29M', ' Android 7.0, Redmi Note 4 Build/NRD90M', ' Android 6.0, QMobile X32 Build/MRA58K', ' Android 5.1.1, SM-J200F Build/LMY47X', ' WOW64, rv:40.0', ' Android 6.0, IK-7216 Build/MRA58K', ' Android 7.0, SM-J710FN Build/NRD90M']

现在我有另一个清单如下:

ww=["http://yandex.com/bots',", "http://ahrefs.com/robot/',", "http://www.google.com/bot.html',", "https://blog.ucoz.ru/upolicy',", "http://sur.ly/bot.html',", "http://www.pinterest.com/bot.html',", "http://www.semrush.com/bot.html',"]

ww中的值也在c11中,如果c11中的值与ww的任何元素中的值匹配/存在,则我要查找这些值并将其替换为“”(空字符串)。(即)ww的任何元素与c11中的值匹配或包含值,我们需要将该值替换为空字符串。你知道吗

就像

for i in ww:
  re.sub(i,'',str(c11))

有人建议使用re模块吗

我期待以下输出:

   c11=[' YandexBot/3.0, ', ' Win64, x64', ' AhrefsBot/5.2, ', ' Android 7.1.2, Redmi 4 Build/N2G47H', ' Android 7.0, EVA-L09 Build/HUAWEIEVA-L09', ' Android 6.0.1, Redmi Note 4 Build/MMB29M', ' Googlebot/2.1, ', ' Android 6.0.1, CPH1701 Build/MMB29M', ' Android 6.0.1, Redmi 4 Build/MMB29M', ' Android 6.0.1, SM-J500F Build/MMB29M', ' uCrawler/1.0 , ', ' SurdotlyBot/1.0, ', ' Opera Mini/8.0.40377/85.73, U', ' Pinterestbot/1.0, ', ' Android 7.0, SM-J701F Build/NRD90M', ' Android 6.0.1, Nexus 5X Build/MMB29P', ' Android 6.0.1, SM-A500H Build/MMB29M', ' Android 7.1.1, SM-T385 Build/NMF26X', ' SemrushBot/1.2~bl, ', ' Android 6.0.1, ONE A2003 Build/MMB29M', ' Android 7.0, Redmi Note 4 Build/NRD90M', ' Android 6.0, QMobile X32 Build/MRA58K', ' Android 5.1.1, SM-J200F Build/LMY47X', ' WOW64, rv:40.0', ' Android 6.0, IK-7216 Build/MRA58K', ' Android 7.0, SM-J710FN Build/NRD90M']

Tags: buildcomhttphtmlwwwbotandroidnote
3条回答

或者使用列表理解:

new_cc = [element if element not in ww else '' for element in cc11]

很简单。你知道吗

只是用re。你知道吗

你知道吗回复sub('+([^]*)','',结构(c11))

就这样。谢谢大家

ww = list(map(lambda x: '' if x in c11 else x, c11))

相关问题 更多 >