使用列表项python查找和替换列表项

时间:2018-02-04 12:19:03

标签: python regex python-3.x

我的列表如下:

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中,如果这些值匹配/存在于ww的任何元素中,我想查找并替换c11中的值''(空字符串)。 (即)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']

5 个答案:

答案 0 :(得分:1)

这很简单。

只需使用re。

re.sub(' +([^] *)','',str(c11))

多数民众赞成。谢谢你们每个人

答案 1 :(得分:0)

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

答案 2 :(得分:0)

或使用列表理解:

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

答案 3 :(得分:0)

我认为你需要,

for index, element in enumerate(c11):
    if element in ww:
        c11[index] = ""

答案 4 :(得分:0)

根据您的需要输出。我这里没有使用re。但这似乎更简单。

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 = [' +' + i.replace("',","") for i in ww]
for i in c11:
  sub = i.split(',')
  pos = c11.index(i)
  for j in ww:
    if sub[1] == j:
      i = i.replace(j, "")
      c11[pos] = i

print(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']