Metoda Python Set intersection()
Příklad
Vraťte sadu, která obsahuje položky, které existují jak v sadě
x
, tak v sadě y
:
x =
{"apple", "banana", "cherry"}
y = {"google",
"microsoft", "apple"}
z = x.intersection(y)
print(z)
Definice a použití
Metoda intersection()
vrací množinu, která obsahuje podobnost mezi dvěma nebo více množinami.
Význam: Vrácená sada obsahuje pouze položky, které existují v obou sadách nebo ve všech sadách, pokud je porovnání provedeno s více než dvěma sadami.
Syntax
set.intersection(set1, set2 ... etc)
Hodnoty parametrů
Parameter | Description |
---|---|
set1 | Required. The set to search for equal items in |
set2 | Optional. The other set to search for equal items in. You can compare as many sets you like. Separate the sets with a comma |
Další příklady
Příklad
Porovnejte 3 sady a vraťte sadu s položkami, které jsou ve všech 3 sadách:
x =
{"a", "b", "c"}
y = {"c",
"d", "e"}
z = {"f",
"g", "c"}
result = x.intersection(y, z)
print(result)