在Python中将列表项值与其他列表中的其他项进行比较

发布于 2024-08-21 17:24:11 字数 512 浏览 5 评论 0原文

我想将一个列表中的值与第二个列表中的值进行比较,并返回第一个列表中但不在第二个列表中的所有值,即

list1 = ['one','two','three','four','five']
list2 = ['one','two','four']

返回“三”和“五”。

我对Python只有一点经验,所以这可能是尝试解决它的一种荒谬而愚蠢的方法,但这就是我到目前为止所做的:

def unusedCategories(self):
    unused = []
    for category in self.catList:
        if category != used in self.usedList:
            unused.append(category)
    return unused

然而,这会抛出一个错误“非序列迭代”,其中我认为一个或两个“列表”实际上并不是列表(两个列表的原始输出与我的第一个示例的格式相同)

I want to compare the values in one list to the values in a second list and return all those that are in the first list but not in the second i.e.

list1 = ['one','two','three','four','five']
list2 = ['one','two','four']

would return 'three' and 'five'.

I have only a little experience with python, so this may turn out to be a ridiculous and stupid way to attempt to solve it, but this what I have done so far:

def unusedCategories(self):
    unused = []
    for category in self.catList:
        if category != used in self.usedList:
            unused.append(category)
    return unused

However this throws an error 'iteration over non-sequence', which I gather to mean that one or both 'lists' aren't actually lists (the raw output for both is in the same format as my first example)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

她比我温柔 2024-08-28 17:24:11

set(list1).difference(set(list2))

set(list1).difference(set(list2))

拥抱我好吗 2024-08-28 17:24:11

使用集合来获取列表之间的差异:

>>> list1 = ['one','two','three','four','five']
>>> list2 = ['one','two','four']
>>> set(list1) - set(list2)
set(['five', 'three'])

Use sets to get the difference between the lists:

>>> list1 = ['one','two','three','four','five']
>>> list2 = ['one','two','four']
>>> set(list1) - set(list2)
set(['five', 'three'])
唐婉 2024-08-28 17:24:11

使用 set.difference

>>> list1 = ['one','two','three','four','five']
>>> list2 = ['one','two','four']
>>> set(list1).difference(list2)
{'five', 'three'}

你可以跳过要设置的 list2 转换。

with set.difference:

>>> list1 = ['one','two','three','four','five']
>>> list2 = ['one','two','four']
>>> set(list1).difference(list2)
{'five', 'three'}

you can skip conversion of list2 to set.

听不够的曲调 2024-08-28 17:24:11

您可以使用集合或列表理解来完成此操作:

unused = [i for i in list1 if i not in list2]

You can do it with sets or a list comprehension:

unused = [i for i in list1 if i not in list2]
中性美 2024-08-28 17:24:11

这里的所有答案都是正确的。如果列表很短,我会使用列表理解;集会更有效率。在探索为什么您的代码不起作用时,我没有收到错误。 (这不起作用,但那是另一个问题)。

>>> list1 = ['a','b','c']
>>> list2 = ['a','b','d']
>>> [c for c in list1 if not c in list2]
['c']
>>> set(list1).difference(set(list2))
set(['c'])
>>> L = list()
>>> for c in list1:
...     if c != L in list2:
...         L.append(c)
... 
>>> L
[]

问题是 if 语句没有任何意义。
希望这有帮助。

All the answers here are correct. I would use list comprehension if the lists are short; sets will be more efficient. In exploring why your code doesn't work, I don't get the error. (It doesn't work, but that's a different issue).

>>> list1 = ['a','b','c']
>>> list2 = ['a','b','d']
>>> [c for c in list1 if not c in list2]
['c']
>>> set(list1).difference(set(list2))
set(['c'])
>>> L = list()
>>> for c in list1:
...     if c != L in list2:
...         L.append(c)
... 
>>> L
[]

The problem is that the if statement makes no sense.
Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文