Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python for循环不';t删除数组中的两个项_Python - Fatal编程技术网

python for循环不';t删除数组中的两个项

python for循环不';t删除数组中的两个项,python,Python,我想删除remo数组中包含以下字符串的所有网站,但是它只删除第一个索引。这是我到目前为止所拥有的 如何删除阵列中的两个项目 到目前为止,它删除了包含“列表”的URL 您只需收集集合中的所有网站,并最终从中减去remo set: filtered_websites = websites - set(["listings", "nationwide"]) 这里的问题是,在URL中找不到“listings”字符串后,它会将其添加到列表中,然后检查其中是否包含“nationwide”。 试试这个:

我想删除
remo
数组中包含以下字符串的所有网站,但是它只删除第一个索引。这是我到目前为止所拥有的

如何删除阵列中的两个项目

到目前为止,它删除了包含“列表”的URL


您只需收集集合中的所有网站,并最终从中减去remo set:

filtered_websites = websites - set(["listings",  "nationwide"])

这里的问题是,在URL中找不到“listings”字符串后,它会将其添加到列表中,然后检查其中是否包含“nationwide”。 试试这个:

for r in remo:
  if r in website:
    break
else: # note: this is indented to the for loop.
  websites.add(website)
这样,它将对remo数组中的每个字执行整个for循环检查。如果它们都不存在,for循环将达到自然结束,else语句将执行。
但是,如果找到remo数组的值,for循环将被中断,else语句将不会执行。

您没有尝试删除任何内容。它为什么会删除具有“listings”字符串的URL?在添加它之前,您需要检查
remo
中的所有字符串是否不在
网站
中:
如果所有(r不在remo中的r的网站中):websites.add(website)
。这现在起作用了,但是我不理解它的逻辑和为什么起作用……似乎是一个过滤器问题,可以通过filter()或列表理解来解决,然后将其放入set()中删除重复项。一般来说,当然。请再次阅读问题:
websites
是一个URL列表。当然-但它包含URL,而不是简单的单词。仅删除数组中的第一项,但如果全部(r不在website中,r在remo中):websites.add(website)
如果你仔细看,我的代码与你的代码略有不同。If-all语句是一个我不知道的速记,所以我很高兴在这里找到它。不用担心,伙计,我使用
If-all(r不在网站中,r在remo中):websites.add(网站)
for r in remo:
  if r in website:
    break
else: # note: this is indented to the for loop.
  websites.add(website)