如何找到交点?

发布于 2025-01-17 13:36:26 字数 1139 浏览 2 评论 0原文

我有两个清单。


    red = [[2,2], [2, 3], [2, 4], [3, 2], [3, 3], [3, 4], [4, 2], [4, 3], [4, 4]]
    
    blue = [[3, 3], [3, 4], [3, 5], [3, 6], [4, 3], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [6, 3], [6, 4], [6, 5], [6, 6]]

在这种情况下如何找到交集?

我知道什么是套装。

但是,在这种情况下......我认为不能使用列表。

列在列表中...我该怎么办?


[[2, 2], [2, 3], [2, 4], [3, 2], [3, 3], [3, 4], [4, 2], [4, 3], [4, 4]]
[[3, 3], [3, 4], [3, 5], [3, 6], [4, 3], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [6, 3], [6, 4], [6, 5], [6, 6]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-e0a8e962ef62> in <module>()
     11     print(list(red))
     12     print(list(blue))
---> 13     intersection = len(list(set(red) & set(blue)))
     14     print(list(set(red) & set(blue)))
     15     print(set(list(red)) & set(list(blue)))

TypeError: unhashable type: 'list'

I have two list.


    red = [[2,2], [2, 3], [2, 4], [3, 2], [3, 3], [3, 4], [4, 2], [4, 3], [4, 4]]
    
    blue = [[3, 3], [3, 4], [3, 5], [3, 6], [4, 3], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [6, 3], [6, 4], [6, 5], [6, 6]]

how to find intersection in this case?

I know what is the set.

but, In this case... I think it can not to do to use list.

list in list... how can i do?


[[2, 2], [2, 3], [2, 4], [3, 2], [3, 3], [3, 4], [4, 2], [4, 3], [4, 4]]
[[3, 3], [3, 4], [3, 5], [3, 6], [4, 3], [4, 4], [4, 5], [4, 6], [5, 3], [5, 4], [5, 5], [5, 6], [6, 3], [6, 4], [6, 5], [6, 6]]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-e0a8e962ef62> in <module>()
     11     print(list(red))
     12     print(list(blue))
---> 13     intersection = len(list(set(red) & set(blue)))
     14     print(list(set(red) & set(blue)))
     15     print(set(list(red)) & set(list(blue)))

TypeError: unhashable type: 'list'

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

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

发布评论

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

评论(1

朮生 2025-01-24 13:36:26

集合只能包含可伸缩的数据,并且由于可变,因此列表无法使用。另一方面,元组是完美的覆盖。

red_set = frozenset(tuple(x) for x in red)
blue_set = frozenset(tuple(x) for x in blue)

intersection_set = red_set & blue_set
intersection_list = list(list(x) for x in intersection_set)

如果您不介意使用元组工作,则可以将其保留为Intersection_set的形式,并且可以正常工作。但是最后一行将其返回到您启动的原始“列表”表示。

请注意,set也可以在这里工作,但是由于我们从不更改它们,frozenset均表明我们的意图更好。

Sets can only contain hashable data, and lists are not hashable since they're mutable. On the other hand, tuples are perfectly hashable.

red_set = frozenset(tuple(x) for x in red)
blue_set = frozenset(tuple(x) for x in blue)

intersection_set = red_set & blue_set
intersection_list = list(list(x) for x in intersection_set)

If you don't mind working with tuples, you can leave it in the form intersection_set and it'll work just fine. But the last line gets it back to the original "list of lists" representation you started with.

Note that set would work here too, but since we never change them, frozenset states our intentions a bit better.

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