简体   繁体   English

搜索不在列表中的内容

[英]Searching for something thats not in a list

Basically I'm trying to search a list from a list (not sure if they are actually lists or dictionary, but I've included the exact input) and if item from list 1 is not in list 2 I want to remove it from list 1, what ever I do all I get is what's in the list and not what's not in the list.基本上我正在尝试从列表中搜索列表(不确定它们实际上是列表还是字典,但我已经包含了确切的输入)并且如果列表 1 中的项目不在列表 2 中,我想将其从列表中删除1,我所做的只是列表中的内容,而不是列表中的内容。

import datetime

listone = {}
listtwo = []
listone["https://www.somesite.com.au/1"]= datetime.datetime.now()
listone["https://www.somesite.com.au/2"]= datetime.datetime.now()
listone["https://www.somesite.com.au/3"]= datetime.datetime.now()

listtwo.append(["http://www.not.com/not1","test 1"])
listtwo.append(["http://www.not.com/not2","test 6"])
listtwo.append(["http://www.not.com/not3","test 5"])
listtwo.append(["http://www.not.com/not4","test 4"])
listtwo.append(["http://www.not.com/not5","test 3"])
listtwo.append(["http://www.not.com/not6","test 2"])
listtwo.append(["https://www.somesite.com.au/2", "test 1"])

temp = listone.copy()
for key, value in temp.items():
    for item in listtwo:
        if item[0] != key:
            listone.pop(item[0], None)
print(listone)

Now I'm only getting back somesite 1 and 3 in listone after the code has ran, which it should be somesite 2 in listone and 1 and 3 should be removed.现在我只在代码运行后返回listone中的somesite 1和3,它应该是listone中的somesite 2,应该删除1和3。 Can someone point out where I'm going wrong please?有人可以指出我要去哪里错了吗?

You need to test if an element is not in the whole list of filtered elements before deciding to remove it.在决定删除它之前,您需要测试一个元素是否不在过滤元素的整个列表中。 There's a simpler solution: first, extract the items that you want to use for filtering.有一个更简单的解决方案:首先,提取要用于过滤的项目。 I'll use a set for efficiency and a generator expression for extracting the URLs:我将使用一set来提高效率,并使用一个生成器表达式来提取 URL:

urls = set(url for url, text in listtwo)

Then, create a new dictionary without the elements that you want to filter;然后,创建一个没有要过滤的元素的新字典; here I'm using a dictionary comprehension:在这里我使用字典理解:

listone = {k: v for k, v in listone.items() if k in urls}

The result will be as expected:结果将如预期:

listone
=> {'https://www.somesite.com.au/2': datetime.datetime(2021, 4, 15, 13, 38, 20, 388197)}

By the way, listone is actually a dictionary, not a list.顺便说一句, listone实际上是字典,而不是列表。

It sounds like you don't quite understand the logic you wrote.听起来你不太明白你写的逻辑。 In your first iteration of the loop, key is equal to https://www.somesite.com.au/1 .在循环的第一次迭代中, key等于https://www.somesite.com.au/1 The nested loop, for item in listtwo will then have item[0] try to pop a value from temp if it does not match https://www.somesite.com.au/1 . for item in listtwo的嵌套循环将让item[0]尝试从temppop一个值,如果它与https://www.somesite.com.au/1不匹配。 In that first iteration none of the values of item[0] will match so your if statement is met for every iteration of the loop.在第一次迭代中, item[0]任何值都不会匹配,因此循环的每次迭代都会满足您的if语句。 None of the values of item[0] are in temp besides the last one, "https://www.somesite.com.au/2" .除了最后一个"https://www.somesite.com.au/2"之外, item[0]的任何值都不在temp中。 Therefore, when you reach the final iteration of the loop it will remove that value from temp .因此,当您到达循环的最后一次迭代时,它将从temp中删除该值。

If we modify the code to print what it plans to pop and what it actually removes you can see what I describe happen.如果我们修改代码以print它计划pop的内容以及实际删除的内容,您可以看到我描述的情况。

Going to try to pop -> http://www.not.com/not1
Actually popped -> None
Going to try to pop -> http://www.not.com/not2
Actually popped -> None
Going to try to pop -> http://www.not.com/not3
Actually popped -> None
Going to try to pop -> http://www.not.com/not4
Actually popped -> None
Going to try to pop -> http://www.not.com/not5
Actually popped -> None
Going to try to pop -> http://www.not.com/not6
Actually popped -> None
Going to try to pop -> https://www.somesite.com.au/2
Actually popped -> 2021-04-15 07:50:10.448479
....

That is the reason your code is not working as expected.这就是您的代码无法按预期工作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM