Guest User

Untitled

a guest
Nov 22nd, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. # Sets revisited in python
  2.  
  3. def union(list1, list2):
  4. result = list(set(list1 + list2))
  5. print("Union of {} and {} is {}".format(list1, list2, result))
  6.  
  7. def intersect(list1, list2):
  8. result = [x for x in list1 if x in list2]
  9. print("Intersection of {} and {} is {}".format(list1, list2, result))
  10.  
  11. def difference(list1, list2):
  12. result = [x for x in list1 if x not in list2]
  13. print("Difference of {} and {} is {}".format(list1, list2, result))
  14.  
  15. def cross_product(list1, list2):
  16. result = [(x * y) for x in list1 for y in list2]
  17. print("Cross product of {} and {} is {}".format(list1, list2, result))
  18.  
  19. def equality(list1, list2):
  20. result = list1 == list2
  21. print("Equality of {} and {} is {}".format(list1, list2, result))
  22.  
  23. def subset(list1, list2):
  24. result = set(list1) < set(list2)
  25. print("Subset of {} and {} is {}".format(list1, list2, result))
  26.  
  27. def proper_subset(list1, list2):
  28. result = set(list1) < set(list2) and list1 != list2
  29. print("Proper subset of {} and {} is {}".format(list1, list2, result))
  30.  
  31. def complement(list1, list2):
  32. result = [x for x in list2 if x not in list1]
  33. print("Complement of {} and {} is {}".format(list1, list2, result))
  34. complement([1,2,3,4], [1,4,2,6])
Add Comment
Please, Sign In to add comment