简体   繁体   中英

How to pass 2 sets as parameters for a function?

Create a Python program that asks the user to enter two sets of comma-separated values. Use the string split() method to parse the line and then use the set() function to covert the lists to sets. Demonstrate set theory for the two sets by displaying the two sets and their relationship to each other as subset, superset, union, intersection, and difference.

Im not sure how to pass two sets in a function?

print(two_set(set(1,2,3,4), set(2,3,4,5,6)))

TypeError: set expected at most 1 arguments, got 4

You should convert it into set and then pass them:

def two_set(set_a, set_b):
        return (set_a, set_b)


set_a = set([1,2,3,4])
set_b = set([2,3,4,5,6,6,6,6])

print(two_set(set_a, set_b))

OUTPUT:

({1, 2, 3, 4}, {2, 3, 4, 5, 6})

this is one of the solutions.

class Settherory:
    def __init__(self, set1,set2):
        self.set1 = set1
        self.set2=set2
    def subset(self):
        if self.set1.issubset(self.set2):
            return '{} is subset of {}'.format(self.set1,self.set2)
        elif self.set2.issubset(self.set1):
            return '{} is subset of {}'.format(self.set2,self.set1)
        else:
            return 'not a subset'

    def superset(self):
        if self.set1.issuperset(self.set2):
            return '{} is superset of {}'.format(self.set1,self.set2)
        elif self.set2.issuperset(self.set1):
            return '{} is superset of {}'.format(self.set2,self.set1)
        else:
            return 'not a superset'
    def union(self):
        return 'union of sets is {}'.format(self.set1 | self.set2)

    def difference(self):
        return 'difference of set is {}'.format(self.set1 - self.set2)

    def intersection(self):
        return 'intersection of two sets is {}'.format(self.set1 & self.set2)



set_1 = set(map(int,input('enter the data in set 1 ').strip().split(',')))
set_2 = set(map(int,input('enter the data in set 2').strip().split(',')))

x= Settherory(set_1, set_2)
print(x.subset(), x.difference(), x.superset(),x.union(),x.intersection(),sep='\n')


'''
enter the data in set 1 1,2,3,4,5

enter the data in set 23,4,5
{3, 4, 5} is subset of {1, 2, 3, 4, 5}
difference of set is {1, 2}
{1, 2, 3, 4, 5} is superset of {3, 4, 5}
union of sets is {1, 2, 3, 4, 5}
intersection of two sets is {3, 4, 5}
'''

another way to do is

# take input as single line seperated by ','
set_1 = set(map(int,input('enter the data in set 1 :').strip().split(',')))
set_2 = set(map(int,input('enter the data in set 2 : ').strip().split(',')))

# for finding the subset or other set operation you can use direct inbuilt function on set like:

print(set_1.issubset(set_2))
print(set_1.issuperset(set_2))
print(set_1.union(set_2))
print(set_1.difference(set_2))

# output 
'''
enter the data in set 1 : 1,2,3,4

enter the data in set 2 : 2,3
False
True
{1, 2, 3, 4}
{1, 4}
'''

the set class takes an array for initializing but you are giving multiple integers instead. Change your code to

print(two_set(set([1,2,3,4]), set([2,3,4,5,6])))

to solve your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM