Created
November 20, 2017 20:10
-
-
Save hmasila/4ead1a1c2a450e136fc9671cd33ef48d to your computer and use it in GitHub Desktop.
sets created by andela_hmasila - https://repl.it/@andela_hmasila/sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Sets revisited in python | |
def union(list1, list2): | |
result = list(set(list1 + list2)) | |
print("Union of {} and {} is {}".format(list1, list2, result)) | |
def intersect(list1, list2): | |
result = [x for x in list1 if x in list2] | |
print("Intersection of {} and {} is {}".format(list1, list2, result)) | |
def difference(list1, list2): | |
result = [x for x in list1 if x not in list2] | |
print("Difference of {} and {} is {}".format(list1, list2, result)) | |
def cross_product(list1, list2): | |
result = [(x * y) for x in list1 for y in list2] | |
print("Cross product of {} and {} is {}".format(list1, list2, result)) | |
def equality(list1, list2): | |
result = list1 == list2 | |
print("Equality of {} and {} is {}".format(list1, list2, result)) | |
def subset(list1, list2): | |
result = set(list1) < set(list2) | |
print("Subset of {} and {} is {}".format(list1, list2, result)) | |
def proper_subset(list1, list2): | |
result = set(list1) < set(list2) and list1 != list2 | |
print("Proper subset of {} and {} is {}".format(list1, list2, result)) | |
def complement(list1, list2): | |
result = [x for x in list2 if x not in list1] | |
print("Complement of {} and {} is {}".format(list1, list2, result)) | |
complement([1,2,3,4], [1,4,2,6]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment