Dmitriy_kzn
Dmitriy_kzn

Reputation: 578

Intersection in sets

I have my_dict with sets as values and I have x which is also a set.

I need to return list with set from my dict which contain all numbers in x. If set in my_dict does not contain all numbers in x I do not want to return it.

I want to use intersection (&) but it returns all the sets in my_dict.

my_dict = {1: {1,2,3,4,5},

       2: {1,2,3,7,8},

       3: {1,2,3,4}

       }

x = {1,2,5}
new_list = []


for i in my_dict:
   if my_dict[i] & x:
        new_list.append(i)
print(new_list)

Output:

[1, 2, 3]

I need to receive [1] instead of [1, 2, 3]

Upvotes: 3

Views: 374

Answers (5)

Annonymous
Annonymous

Reputation: 978

To check whether the entirety of a set is within another set, the nicest (in my opinon) way is to use the < and > operators, which are override to act as the equivalent of "is a superset of" in mathematics, and equivalent to the set.issuperset method. The advantage of this way is that the >= and <= operators are naturally available to check non-strict supersets.

Here's quite an idomatic way of doing it:

new_list = []
for key, value in my_dict.items():
    if value >= x:
        new_list.append(key)

The problem with your original code is it checks to see if there is any intersection between the two sets, i.e. they share even just one element, when you seem to want to check if all of x: set is in the set you're checking against.

I would also advise using a list compehension if you want to simplify the code, unless you have other steps you also need to do.

new_list = [key for key, value in my_dict.items() if value >= x]

Upvotes: 0

mrhallak
mrhallak

Reputation: 1178

This can also be solved using the issubset function. Here's an example:

for i in my_dict:
     if x.issubset(my_dict[i]):
            new_list.append(i)

Output: [1]

In this example, we're checking whether the value of every key value pair in the dictionary is a super-set of x (in other words x belongs to my_dict[i]), if that is the case then we just append the index to the desired list.

Upvotes: 0

Rajan Chauhan
Rajan Chauhan

Reputation: 1375

When intersection becomes x that means all values in x are present in the set in dictionary.

for i in my_dict:
     if (my_dict[i] & x)==x:
         new_list.append(i)
print(new_list)

Edit: as suggested in the comments below you can also do

for i in my_dict:
     if x.issubset(my_dict[i]):
         new_list.append(i)
print(new_list)

Upvotes: 2

Vibhutha Kumarage
Vibhutha Kumarage

Reputation: 1399

The inter section between my_dict values and x should be equal to x that means x should be a subset of my_dict value

my_dict = {1: {1,2,3,4,5},
           2: {1,2,3,7,8},
           3: {1,2,3,4}}

x = {1,2,5}

new_list = []

for i,j in my_dict.items():
   if x.issubset(j):
        new_list.append(i)

print(new_list)

Upvotes: 1

Blckknght
Blckknght

Reputation: 104712

I suggest you use the set.issuperset method, rather than using the & operator. Why combine several operators when a method exists to do exactly what you want?

new_list = []
for i in my_dict:
    if my_dict[i].issuperset(x):
        new_list.append(i)

Note that I'd normally write this with a list comprehension:

newlist = [key for key, value in my_dict.items() if value.issuperset(x)]

Upvotes: 1

Related Questions