Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. # Take two lists, say for example these two:
  2.  
  3. # a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
  4. # b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  5. # and write a program that returns a list that contains only the elements that are common between the lists (without duplicates).
  6. # Make sure your program works on two lists of different sizes.
  7.  
  8. #Python code to generate
  9. # random numbers and
  10. # append them to a list
  11. import random
  12.  
  13. # Function to generate
  14. # and append them
  15. # start = starting range,
  16. # end = ending range
  17. # num = number of
  18. # elements needs to be appended
  19. def Rand(start, end, num):
  20. res = []
  21.  
  22. for j in range(num):
  23. res.append(random.randint(start, end))
  24.  
  25. return res
  26.  
  27. # Driver Code
  28. num = int(input("Enter the range for first list : "))
  29. start = int(input("Enter the starting number : "))
  30. end = int(input("ENter the ending number : "))
  31. list1 = Rand(start, end, num)
  32. print(list1)
  33.  
  34. num2 = int(input("Enter the range for second list : "))
  35. start2 = int(input("Enter the starting number : "))
  36. end2 = int(input("ENter the ending number : "))
  37. list2 = Rand(start2, end2, num2)
  38. print(list2)
  39.  
  40. final = []
  41.  
  42. for i in list1:
  43. if i in list2:
  44. final.append(i)
  45.  
  46. print("The list containing common integers : ")
  47. set = set(final)
  48. list = list(set)
  49.  
  50. print(list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement