我正在嘗試從我的獲取串列中過濾一些 url,但它沒有應用過濾器,它給了我相同的輸出fetchurl
,weblinks
這是我的代碼
weblinks =[]
fetchurl = ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/']
filterlist = ["youtube.com","twitter.com","facebook.com","google.com","tiktok.com"]
for ur in fetchurl:
for i in range(len(filterlist)):
if filterlist[i] not in ur:
weblinks.append(ur)
print("weblinks url working", weblinks)
輸出
weblinks url working ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979', 'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/']
uj5u.com熱心網友回復:
錯誤是,如果 中的任何值不匹配,則您正在添加元素filterlist
,但如果所有值都不匹配,則應該添加元素。如果至少找到一個匹配項,則內部回圈將提前退出,并且該元素不會包含在結果串列中。如果沒有匹配項,則回圈將完整運行并運行else:
,然后該元素將包含在最終串列中。
weblinks = []
fetchurl = ['https://www.youtube.com/watch?v=___nkvwpnAg&t=1218',
'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
'https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979',
'https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/']
filterlist = ["youtube.com", "twitter.com", "facebook.com", "google.com", "tiktok.com"]
for ur in fetchurl:
for i in filterlist:
if i in ur:
break
else:
weblinks.append(ur)
print("weblinks url working", *weblinks,sep='\n')
weblinks url working
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://www.farandwide.com/s/fascinating-facts-every-country-7c1f1a0efdf64979
https://wonderfulwanderings.com/interesting-facts-about-the-netherlands/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529028.html
標籤:Pythonfor循环if 语句
下一篇:回圈檔案中的行,從第二列創建串列