Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- x = range(1,10)
- y = range(8,20)
- (The answer I am looking for would be the integers 8 and 9.)
- >>> x = range(1,10)
- >>> y = range(8,20)
- >>> xs = set(x)
- >>> ys = set(y)
- >>> xs.intersection(ys)
- set([8, 9])
- >>> x = range(1,10)
- >>> y = range(8,20)
- >>> list(set(x) & set(y))
- [8, 9]
- range(max(x[0], y[0]), min(x[-1], y[-1])+1)
- x = range(1,10)
- y = range(8,20)
- z = [i for i in x if i in y]
- print z
- for a,b,c,d in ((1,10,10,14),
- (1,10,9,14),
- (1,10,4,14),
- (1,10,4,10),
- (1,10,4,9),
- (1,10,4,7),
- (1,10,1,7),
- (1,10,-3,7),
- (1,10,-3,2),
- (1,10,-3,1),
- (1,10,-11,-5)):
- x = range(a,b)
- y = range(c,d)
- print 'x==',x
- print 'y==',y
- b = not ((x[-1]<y[0]) or (y[-1]<x[0]))
- print ' x %s y' % ("doesn't overlap"," OVERLAPS ")[b]
- print
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [10, 11, 12, 13]
- x doesn't overlap y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [9, 10, 11, 12, 13]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [4, 5, 6, 7, 8, 9]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [4, 5, 6, 7, 8]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [4, 5, 6]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [1, 2, 3, 4, 5, 6]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [-3, -2, -1, 0, 1]
- x OVERLAPS y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [-3, -2, -1, 0]
- x doesn't overlap y
- x== [1, 2, 3, 4, 5, 6, 7, 8, 9]
- y== [-11, -10, -9, -8, -7, -6]
- x doesn't overlap y
- from time import clock
- x = range(-12,15)
- y = range(-5,3)
- te = clock()
- for i in xrange(100000):
- w = set(x).intersection(y)
- print ' set(x).intersection(y)',clock()-te
- te = clock()
- for i in xrange(100000):
- w = range(max(x[0], y[0]), min(x[-1], y[-1])+1)
- print 'range(max(x[0], y[0]), min(x[-1], y[-1])+1)',clock()-te
- set(x).intersection(y) 0.951059981087
- range(max(x[0], y[0]), min(x[-1], y[-1])+1) 0.377761978129
Add Comment
Please, Sign In to add comment