Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Khác biệt giữa Operator và Method trên Python Set

Set operations (union, intersection, difference, symmetric difference) có thể dùng method hoặc operator.

Union

s1={1,2,3,4,5}; s2={4,5,6,7,8}
s1.union(s2)      # {1,2,3,4,5,6,7,8}
s1 | s2           # {1,2,3,4,5,6,7,8}

Intersection

s1.intersection(s2)  # {4,5}
s1 & s2              # {4,5}

Difference

s1.difference(s2)  # {1,2,3}
s1 - s2            # {1,2,3}

Method chấp nhận bất kỳ iterable nào, operator yêu cầu cả hai là set.