我如何告诉 python 不要打印列表中的项目?
How do I tell python to NOT print an item in a list?
我的 python 脚本解析来自多个 RSS 提要的标题和链接。我将这些标题存储在一个列表中,并且我想确保我从不打印重复项。我该怎么做?
#!/usr/bin/python
from twitter import *
from goose import Goose
import feedparser
import time
from pyshorteners import Shortener
import pause
import newspaper
dr = feedparser.parse("http://www.darkreading.com/rss_simple.asp")
sm =feedparser.parse("http://www.securitymagazine.com/rss/topic/2654-cyber-tactics.rss")
dr_posts =["CISO Playbook: Games of War & Cyber Defenses",
"SWIFT Confirms Cyber Heist At Second Bank; Researchers Tie Malware Code to Sony Hack","The 10 Worst Vulnerabilities of The Last 10 Years",
"GhostShell Leaks Data From 32 Sites In 'Light Hacktivism' Campaign",
"OPM Breach: 'Cyber Sprint' Response More Like A Marathon",
"Survey: Customers Lose Trust In Brands After A Data Breach",
"Domain Abuse Sinks 'Anchors Of Trust'",
"The 10 Worst Vulnerabilities of The Last 10 Years",
]
sm_posts = ["10 Steps to Building a Better Cybersecurity Plan"]
x = 1
while True:
try:
drtitle = dr.entries[x]["title"]
drlink = dr.entries[x]["link"]
if drtitle in dr_posts:
x += 1
drtitle = dr.entries[x]["title"]
drtitle = dr.entries[x]["link"]
print drtitle + "\n" + drlink
dr_posts.append(drtitle)
x -= 1
pause.seconds(10)
else:
print drtitle + "\n" + drlink
dr_posts.append(drtitle)
pause.seconds(10)
smtitle = sm.entries[x]["title"]
smlink = sm.entries[x]["link"]
if smtitle in sm_posts:
x +=1
smtitle = sm.entries[x]["title"]
smtitle = sm.entries[x]["title"]
print smtitle + "\n" + smlink
sm_posts.append(smtitle)
pause.seconds(10)
else:
print smtitle + "\n" + smlink
sm_posts.append(smtitle)
x+=1
pause.seconds(10)
except IndexError:
print "FAILURE"
break
暂时我只有跳过条目。这将是一个问题,因为如果 RSS 提要中的行下方还有另一个重复项,那么我将有更多重复项。
您可以利用数据结构 set,因为 "uniqueness" 的 属性 将为您完成这项工作。从本质上讲,我们可以让您的列表成为一个集合,然后再将其设置为一个列表,这样可以确保您的列表现在填充了严格唯一的值。
如果你有一个列表 l,那么你可以通过
使它独一无二
l = list(set(l))
如果您不想重复打印 link,您可以使用 counter or defaultdict
sm_posts = defaultdict(int)
sm_posts[sm_links] += 1
print sm_posts.keys() #will print all the unique links
好处是您还可以通过执行
来计算 link 的重复次数
sm_posts[sm_links]
>>> link_counts
试一试。
我的 python 脚本解析来自多个 RSS 提要的标题和链接。我将这些标题存储在一个列表中,并且我想确保我从不打印重复项。我该怎么做?
#!/usr/bin/python
from twitter import *
from goose import Goose
import feedparser
import time
from pyshorteners import Shortener
import pause
import newspaper
dr = feedparser.parse("http://www.darkreading.com/rss_simple.asp")
sm =feedparser.parse("http://www.securitymagazine.com/rss/topic/2654-cyber-tactics.rss")
dr_posts =["CISO Playbook: Games of War & Cyber Defenses",
"SWIFT Confirms Cyber Heist At Second Bank; Researchers Tie Malware Code to Sony Hack","The 10 Worst Vulnerabilities of The Last 10 Years",
"GhostShell Leaks Data From 32 Sites In 'Light Hacktivism' Campaign",
"OPM Breach: 'Cyber Sprint' Response More Like A Marathon",
"Survey: Customers Lose Trust In Brands After A Data Breach",
"Domain Abuse Sinks 'Anchors Of Trust'",
"The 10 Worst Vulnerabilities of The Last 10 Years",
]
sm_posts = ["10 Steps to Building a Better Cybersecurity Plan"]
x = 1
while True:
try:
drtitle = dr.entries[x]["title"]
drlink = dr.entries[x]["link"]
if drtitle in dr_posts:
x += 1
drtitle = dr.entries[x]["title"]
drtitle = dr.entries[x]["link"]
print drtitle + "\n" + drlink
dr_posts.append(drtitle)
x -= 1
pause.seconds(10)
else:
print drtitle + "\n" + drlink
dr_posts.append(drtitle)
pause.seconds(10)
smtitle = sm.entries[x]["title"]
smlink = sm.entries[x]["link"]
if smtitle in sm_posts:
x +=1
smtitle = sm.entries[x]["title"]
smtitle = sm.entries[x]["title"]
print smtitle + "\n" + smlink
sm_posts.append(smtitle)
pause.seconds(10)
else:
print smtitle + "\n" + smlink
sm_posts.append(smtitle)
x+=1
pause.seconds(10)
except IndexError:
print "FAILURE"
break
暂时我只有跳过条目。这将是一个问题,因为如果 RSS 提要中的行下方还有另一个重复项,那么我将有更多重复项。
您可以利用数据结构 set,因为 "uniqueness" 的 属性 将为您完成这项工作。从本质上讲,我们可以让您的列表成为一个集合,然后再将其设置为一个列表,这样可以确保您的列表现在填充了严格唯一的值。
如果你有一个列表 l,那么你可以通过
使它独一无二l = list(set(l))
如果您不想重复打印 link,您可以使用 counter or defaultdict
sm_posts = defaultdict(int)
sm_posts[sm_links] += 1
print sm_posts.keys() #will print all the unique links
好处是您还可以通过执行
来计算 link 的重复次数sm_posts[sm_links]
>>> link_counts
试一试。