我的 python 代碼如果條件給了我磨損的結果與其條件相反的結果。在這里,我想列印不以 youtube.com 開頭或包含 youtube.com 但其列印完整串列的鏈接
fetchurl = ["https://www.smithsonianmag.com/science-nature/how-wolves-really-became-dogs-180970014/",
"https://www.youtube.com/watch?v=DdPMV7GO8q0",
"https://www.npr.org/2013/01/25/170267847/canine-mystery-how-dogs-became-mans-best-friend",
"https://www.youtube.com/watch?v=DdPMV7GO8q0",
"https://www.youtube.com/watch?v=nDt0HKSdRRw&t=554",
"https://www.youtube.com/watch?v=nDt0HKSdRRw&t=554",
"https://www.youtube.com/watch?v=DdPMV7GO8q0",
"https://www.bbc.com/news/science-environment-40638584",
"https://www.youtube.com/watch?v=nDt0HKSdRRw&t=79",
"https://www.bbc.com/news/science-environment-40638584",
"https://www.newscientist.com/article/2264329-humans-may-have-domesticated-dogs-by-accident-by-sharing-excess-meat/",
"https://www.mypet.com/basic-pet-care/dog-mans-best-friend.aspx#:~:text=Scientists speculate that friendship bloomed,Those Who Must Be Obeyed.",
"https://www.youtube.com/watch?v=tggdERc8E6Y&t=11",
"https://www.npr.org/2013/01/25/170267847/canine-mystery-how-dogs-became-mans-best-friend"]
for i in range(len(fetchurl)):
url = random.choice(fetchurl)
if url != fetchurl[i].startswith("https://www.youtube.com/"):
print(url)
else:
print("lol")
uj5u.com熱心網友回復:
如果要列印所有"links that not start with youtube.com or include youtube.com "
for url in fetchurl:
if "https://www.youtube.com/" not in url:
print(url)
以上可能不排除一切。如果是這樣試試這個:
for url in fetchurl:
if "youtube.com" not in url:
print(url)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/526645.html
標籤:Pythonfor循环if 语句