我想删除remo
数组中包含以下字符串的所有网站,但它只会删除第一个索引。这是我到目前为止所拥有的。
我如何能够删除数组中的两个项目?
到目前为止,它删除了包含“listing”的网址
with open('example.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter= ",")
websites = set()
phonenumbers = set()
data = ["gutter"]
# remove urls that have these strings, only works with the first string but not second one.
remo = ["listings", "nationwide"]
for row in readCSV:
website = row[2]
phonenumber = row[0]
if website not in websites:
for x in data:
if x in website:
for r in remo:
if r not in website:
websites.add(website)
答案 0 :(得分:2)
这里的问题是,在URL中找不到“listing”字符串后,它会将其添加到列表中,然后检查“全国范围”是否在其中。 试试这个:
for r in remo:
if r in website:
break
else: # note: this is indented to the for loop.
websites.add(website)
这样它将执行整个for循环 - 检查remo数组中的每个单词。如果它们都不存在,for循环将达到自然结束,else语句将执行。 但是,如果找到remo数组的值,for循环将被破坏,else语句将不会执行。
答案 1 :(得分:-1)
你可以收集所有网站集合,最后从中减去remo集合:
filtered_websites = websites - set(["listings", "nationwide"])