a = [(1,),(3,),(4,),(5,),(7,)] b = [(1,),(2,),(4,),(5,),(8,),(9,)] def iterate_IDs(set1, set2): def next_or_None(seq): try: return seq.next() except StopIteration: return None id1 = None id2 = None id1 = next_or_None(set1) id2 = next_or_None(set2) while id1 is not None or id2 is not None: if id1 is None: yield (None, id2) id2 = next_or_None(set2) elif id2 is None: yield (id1, None) id1 = next_or_None(set2) elif id1 == id2: yield (id1, id2) id1 = next_or_None(set1) id2 = next_or_None(set2) elif id1 > id2: yield (None, id2) id2 = next_or_None(set2) elif id2 > id1: yield (id1, None) id1 = next_or_None(set1) raise StopIteration, "Both sequences run out" import itertools as it for id1,id2 in iterate_IDs(it.chain.from_iterable(a),it.chain.from_iterable(b)): print id1, id2
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question