Location>code7788 >text

The Best Object-Oriented Programming Tutorials on the Web for Getting Started: 43 Python Common Composite Data Types - Extended Built-in Data Types

Popularity:939 ℃/2024-09-02 00:25:39
from typing import List class Set. def __init__(self, value:List[int]=[]):: _# Create a list _ = [] _# Splice the list of values _ (value) _# Find the intersection _ def intersect(self, other:List[int]). _# Create an empty temporary list _ res = [] for x in . _# Insert into empty list if x is the same element _ if x in other. (x) _# Return a new set _ return Set(res) _# Find the union _ def union(self, other:List[int]). _# res is a copy of the list _ res = [:] for x in other. _# for x in other. if not x in res. _# # Insert into res list if element x is not in res list _ (x) _# # Return a new set _ return Set(res) _# Merge method_ def concat(self, value:List[int]). for x in value. if not x in . _# If element x is not in the data list, insert it into the data list _ (x) _# The following are overloaded methods _ _# __len__ to find its length_. def __len__(self). return len() _# len(self)_ _# The __getitem__ method is used to retrieve the elements of an object by index or slice and returns the corresponding value _# def __getitem__(self, key): return [key]. return [key] _# Operator & overload, intersection_. def __and__(self, other). return (other) _# Operator|overloading, ensemble_. def __or__(self, other): return (other) _# Operator | overloading. return (other) _# When printing the instantiated object of a class directly, the system will output the object's self-description information _ def __repr__(self). return 'Set:' + repr() if __name__ == '__main__'. x = Set([1, 3, 5, 7]) y = Set([1, 3, 6, 9]) print(x, y, "len x : ",len(x), "len y : ",len(y)) _# Find the ensemble _ print((Set([1, 4, 7])))) _# Find the intersection_. print((Set([1, 4, 7]))) _# Find the intersection of x and y. _# Print(x & y). print(x & y) _# Find the union of x and y _# print(x | y) print(x | y)