简体   繁体   中英

Find and replace list items with list item python

I have list as follows:

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

Now i have another list as follows:

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',"]

The values in ww is also in c11 , i want to find and replace the values in c11 with ''(empty string) if those values are matches/exists in any element of ww. (ie) any element of ww matches with or contains values in c11, we need to replace the value to an empty string.

like

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

Can any body suggest using re module

I am expecting following output :

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

It was quit simple .

just using re .

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

Thats it . Thanks every one

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

或者使用列表理解:

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

I think you need,

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

This outputs as you want it to be. I didn't use re here. But this seems to be simpler.

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)

Output:

[' 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM