如何找到交点?
我有两个清单。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
集合只能包含可伸缩的数据,并且由于可变,因此列表无法使用。另一方面,元组是完美的覆盖。
如果您不介意使用元组工作,则可以将其保留为
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.
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.