def test_zip_two_lists(self): try: from __pypy__ import specialized_zip_2_lists except ImportError: specialized_zip_2_lists = zip else: raises(TypeError, specialized_zip_2_lists, [], ()) raises(TypeError, specialized_zip_2_lists, (), []) assert specialized_zip_2_lists([], []) == [] assert specialized_zip_2_lists([2, 3], []) == [] assert specialized_zip_2_lists([2, 3], [4, 5, 6]) == [(2, 4), (3, 5)] assert specialized_zip_2_lists([4.1, 3.6, 7.2], [2.3, 4.8]) == [(4.1, 2.3), (3.6, 4.8)] assert specialized_zip_2_lists(["foo", "bar"], [6, 2]) == [("foo", 6), ("bar", 2)]
def test_zip_two_lists(self): try: from __pypy__ import specialized_zip_2_lists except ImportError: specialized_zip_2_lists = zip raises(TypeError, specialized_zip_2_lists, [], ()) raises(TypeError, specialized_zip_2_lists, (), []) assert specialized_zip_2_lists([], []) == [ ] assert specialized_zip_2_lists([2, 3], []) == [ ] assert specialized_zip_2_lists([2, 3], [4, 5, 6]) == [ (2, 4), (3, 5)] assert specialized_zip_2_lists([4.1, 3.6, 7.2], [2.3, 4.8]) == [ (4.1, 2.3), (3.6, 4.8)] assert specialized_zip_2_lists(["foo", "bar"], [6, 2]) == [ ("foo", 6), ("bar", 2)]
def zip(*sequences): """zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.""" l = len(sequences) if l == 2: # A very fast path if the two sequences are lists seq0 = sequences[0] seq1 = sequences[1] try: return specialized_zip_2_lists(seq0, seq1) except TypeError: pass # This is functionally the same as the code below, but more # efficient because it unrolls the loops over 'sequences'. # Only for two arguments, which is the most common case. iter0 = iter(seq0) iter1 = iter(seq1) hint = min( 100000000, # max 100M operator._length_hint(seq0, 0), operator._length_hint(seq1, 0)) with _ManagedNewlistHint(hint) as result: while True: try: item0 = next(iter0) item1 = next(iter1) except StopIteration: return result result.append((item0, item1)) if l == 0: return [] # Gather the iterators and guess the result length (the min of the # input lengths). If any of the iterators doesn't know its length, # we use 0 (instead of ignoring it and using the other iterators; # see lib-python's test_builtin.test_zip). iterators = [] hint = 100000000 # max 100M for seq in sequences: iterators.append(iter(seq)) hint = min(hint, operator._length_hint(seq, 0)) with _ManagedNewlistHint(hint) as result: while True: try: items = [next(it) for it in iterators] except StopIteration: return result result.append(tuple(items))
def zip(*sequences): """zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] Return a list of tuples, where each tuple contains the i-th element from each of the argument sequences. The returned list is truncated in length to the length of the shortest argument sequence.""" l = len(sequences) if l == 2: # A very fast path if the two sequences are lists seq0 = sequences[0] seq1 = sequences[1] try: return specialized_zip_2_lists(seq0, seq1) except TypeError: pass # This is functionally the same as the code below, but more # efficient because it unrolls the loops over 'sequences'. # Only for two arguments, which is the most common case. iter0 = iter(seq0) iter1 = iter(seq1) hint = min(100000000, # max 100M operator._length_hint(seq0, 0), operator._length_hint(seq1, 0)) with _ManagedNewlistHint(hint) as result: while True: try: item0 = next(iter0) item1 = next(iter1) except StopIteration: return result result.append((item0, item1)) if l == 0: return [] # Gather the iterators and guess the result length (the min of the # input lengths). If any of the iterators doesn't know its length, # we use 0 (instead of ignoring it and using the other iterators; # see lib-python's test_builtin.test_zip). iterators = [] hint = 100000000 # max 100M for seq in sequences: iterators.append(iter(seq)) hint = min(hint, operator._length_hint(seq, 0)) with _ManagedNewlistHint(hint) as result: while True: try: items = [next(it) for it in iterators] except StopIteration: return result result.append(tuple(items))