Python Stel kruising() Metode
Voorbeeld
Gee 'n stel terug wat die items bevat wat bestaan in beide stel
x
en stel y
:
x =
{"apple", "banana", "cherry"}
y = {"google",
"microsoft", "apple"}
z = x.intersection(y)
print(z)
Definisie en gebruik
Die intersection()
metode gee 'n stel terug wat die ooreenkoms tussen twee of meer stelle bevat.
Betekenis: Die teruggekeerde stel bevat slegs items wat in beide stelle bestaan, of in alle stelle as die vergelyking met meer as twee stelle gedoen word.
Sintaksis
set.intersection(set1, set2 ... etc)
Parameterwaardes
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 |
Meer voorbeelde
Voorbeeld
Vergelyk 3 stelle en gee 'n stel terug met items wat in al 3 stelle teenwoordig is:
x =
{"a", "b", "c"}
y = {"c",
"d", "e"}
z = {"f",
"g", "c"}
result = x.intersection(y, z)
print(result)