Guest User

Untitled

a guest
Jul 23rd, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. x = range(1,10)
  2. y = range(8,20)
  3.  
  4. (The answer I am looking for would be the integers 8 and 9.)
  5.  
  6. >>> x = range(1,10)
  7. >>> y = range(8,20)
  8. >>> xs = set(x)
  9. >>> ys = set(y)
  10. >>> xs.intersection(ys)
  11. set([8, 9])
  12.  
  13. >>> x = range(1,10)
  14. >>> y = range(8,20)
  15. >>> list(set(x) & set(y))
  16. [8, 9]
  17.  
  18. range(max(x[0], y[0]), min(x[-1], y[-1])+1)
  19.  
  20. x = range(1,10)
  21. y = range(8,20)
  22.  
  23. z = [i for i in x if i in y]
  24. print z
  25.  
  26. for a,b,c,d in ((1,10,10,14),
  27. (1,10,9,14),
  28. (1,10,4,14),
  29. (1,10,4,10),
  30. (1,10,4,9),
  31. (1,10,4,7),
  32. (1,10,1,7),
  33. (1,10,-3,7),
  34. (1,10,-3,2),
  35. (1,10,-3,1),
  36. (1,10,-11,-5)):
  37. x = range(a,b)
  38. y = range(c,d)
  39. print 'x==',x
  40. print 'y==',y
  41. b = not ((x[-1]<y[0]) or (y[-1]<x[0]))
  42. print ' x %s y' % ("doesn't overlap"," OVERLAPS ")[b]
  43. print
  44.  
  45. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  46. y== [10, 11, 12, 13]
  47. x doesn't overlap y
  48.  
  49. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  50. y== [9, 10, 11, 12, 13]
  51. x OVERLAPS y
  52.  
  53. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  54. y== [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
  55. x OVERLAPS y
  56.  
  57. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  58. y== [4, 5, 6, 7, 8, 9]
  59. x OVERLAPS y
  60.  
  61. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  62. y== [4, 5, 6, 7, 8]
  63. x OVERLAPS y
  64.  
  65. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  66. y== [4, 5, 6]
  67. x OVERLAPS y
  68.  
  69. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  70. y== [1, 2, 3, 4, 5, 6]
  71. x OVERLAPS y
  72.  
  73. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  74. y== [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
  75. x OVERLAPS y
  76.  
  77. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  78. y== [-3, -2, -1, 0, 1]
  79. x OVERLAPS y
  80.  
  81. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  82. y== [-3, -2, -1, 0]
  83. x doesn't overlap y
  84.  
  85. x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
  86. y== [-11, -10, -9, -8, -7, -6]
  87. x doesn't overlap y
  88.  
  89. from time import clock
  90.  
  91. x = range(-12,15)
  92. y = range(-5,3)
  93. te = clock()
  94. for i in xrange(100000):
  95. w = set(x).intersection(y)
  96. print ' set(x).intersection(y)',clock()-te
  97.  
  98.  
  99. te = clock()
  100. for i in xrange(100000):
  101. w = range(max(x[0], y[0]), min(x[-1], y[-1])+1)
  102. print 'range(max(x[0], y[0]), min(x[-1], y[-1])+1)',clock()-te
  103.  
  104. set(x).intersection(y) 0.951059981087
  105. range(max(x[0], y[0]), min(x[-1], y[-1])+1) 0.377761978129
Add Comment
Please, Sign In to add comment