Skip to content

Instantly share code, notes, and snippets.

@nitori
Last active March 23, 2016 21:36
Show Gist options
  • Save nitori/68c979c8e88607502db5 to your computer and use it in GitHub Desktop.
Save nitori/68c979c8e88607502db5 to your computer and use it in GitHub Desktop.
class szip:
def __init__(self, *sequences):
self.sequences = sequences
def __len__(self):
return min(len(seq) for seq in self.sequences)
def __getitem__(self, index):
if isinstance(index, slice):
return szip(*(seq[index] for seq in self.sequences))
else:
return tuple(seq[index] for seq in self.sequences)
def __contains__(self, item):
return item in zip(*self.sequences)
def __iter__(self):
return zip(*self.sequences)
def __repr__(self):
return '<szip object at 0x{:x}>'.format(id(self))
@nitori
Copy link
Author

nitori commented Mar 23, 2016

>>> from szip import szip
>>> import random
>>> 
>>> a = [1,2,3,4,5]
>>> b = [4,5,6,7]
>>> z = szip(a, b)
>>> len(z)
4
>>> 
>>> random.choice(z)
(2, 5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment