python中的三个集合的交集?

编程入门 行业动态 更新时间:2024-10-26 05:28:56
本文介绍了python中的三个集合的交集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前,我一直在努力寻找三个集合的交集.现在,这些集合实际上是我要转换为集合,然后试图找到其交集的列表.

Currently I am stuck trying to find the intersection of three sets. Now these sets are really lists that I am converting into sets, and then trying to find the intersection of.

这是我到目前为止所拥有的:

Here's what I have so far:

for list1 in masterlist: list1 = thingList1 for list2 in masterlist: list2 = thingList2 for list3 in masterlist: list3 = thingList3 d3 = [set(thingList1), set(thingList2), set(thingList3)] setmatches c = set.intersection(*map(set,d3)) print setmatches

我得到了

set([]) Script terminated.

我知道有一种更简单更好的方法,但是我找不到一个...

I know there's a much simpler and better way to do this, but I can't find one...

编辑

好的,这就是我现在所拥有的.

Okay, here's what I have now.

setList=() setList2=() setList3=() for list1 in masterlist: setList=list1 for list2 in masterlist: setList2=list2 for list3 in masterlist: setList3=list3 setmatches=set(setList) & set(setList2) & set(setList3) print setmatches

仍然没有给我我想要的东西:这是我确保在每个列表中都有的一个.它给了我看起来像所有集合的加法.

Still doesn't give me what I'm looking for: which is the one match I ensured was in each list. It's giving me what looks like an addition of all the sets.

推荐答案

我认为您只是在寻找:

set(thingList1) & set(thingList2) & set(thingList3)

&符在Python(以及其他一些语言)中是交集.

The ampersand is intersection in Python (and some other languages as well).

更多推荐

python中的三个集合的交集?

本文发布于:2023-11-29 18:32:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!