Malte Paulmann
Malte Paulmann

Reputation: 11

Is there a way to add an intersection to a set in python?

For example:

A=set(frozenset[x,x+1]for x in range (10))
B=set()
C=set()
Result=set()
for B in A:
      for C in A:
            if B!=C:
                 Result.add(frozenset(B.intersection(C)))

#Error: descriptor 'intersection' for 'frozenset' objects doesn't apply to a 'types.GenericAlias' object

Upvotes: 0

Views: 641

Answers (2)

Malte Paulmann
Malte Paulmann

Reputation: 11

Thank you @"user2357112 supports Monica", that was infact the Problem... if I interchange it with

frozenset({x,x+1})

it totally works.

Upvotes: 1

marky004
marky004

Reputation: 67

You can just use the .intersection() method:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.intersection(y)

print(z)

Upvotes: 3

Related Questions