Reputation: 61
I have a list of some numbers:
l1 = [1,2,3,4,5,6,7]
and another one:
l2 = [3,5,6]
I wanna get the list of intervals with numbers which exist are in the l2, but not in l1:
intervals = [[1,2],[4],[7]]
I've tried to do it like this:
current_common_line_no = 0
for line in l1:
if line in l2:
current_common_line_no = line
else:
next_common_line_no = l2[(l2.index(current_common_line_no))+1]
print next_common_line_no
to get list of interval edges, but what next?
Upvotes: 2
Views: 113
Reputation: 1
Use sets. Python has built in set data structures. What you are looking for is difference. Here is the documentation.
So set.difference(x,y)
https://docs.python.org/2/library/sets.html
Let me know if you need more than this
Upvotes: -1
Reputation: 214957
You can use groupby()
with list-comprehension:
from itertools import groupby
[list(g) for k, g in groupby(l1, key=lambda x: x not in l2) if k]
# [[1, 2], [4], [7]]
Upvotes: 6