Answers for "find union of two sets python"

2

union find python

class UnionFind:
    def __init__(self) -> None:
        super().__init__()
        self.representor = {}

    def make_set(self, key):
        self.representor[key] = key

    def find(self, key):
        return self.representor[key]

    def union(self, x, y):
        x_r, y_r = self.find(x), self.find(y)
        for k, r in self.representor.items():
            if r == y_r:
                self.representor[k] = x_r
Posted by: Guest on October-01-2021
0

list union python

set1 = {2, 4, 5, 6}  
set2 = {4, 6, 7, 8}  
set3 = {7, 8, 9, 10} 
  
# union of two sets 
print("set1 U set2 : ", set1.union(set2)) 
  
# union of three sets 
print("set1 U set2 U set3 :", set1.union(set2, set3)) 


>>>

set1 U set2 :  {2, 4, 5, 6, 7, 8}
set1 U set2 U set3 : {2, 4, 5, 6, 7, 8, 9, 10}
Posted by: Guest on March-12-2020

Python Answers by Framework

Browse Popular Code Answers by Language