Skip to content

Commit 8072422

Browse files
committed
Resolves #53 by fixing the behavior of zip_with_index
1 parent 2bac1af commit 8072422

File tree

5 files changed

+16
-9
lines changed

5 files changed

+16
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
### Bug Fixes
1212
* Fixed case where `_wrap` is changing named tuples to arrays when it should preserve them
1313
* Fixed documentation on `to_file` which incorrectly copied from `seq.open` delimiter parameter
14+
* Fixed `Sequence.zip_with_index` behavior. used to mimic `enumerate` by zipping on the left size
15+
while scala and spark do zip on the right side. This introduces different behavior and more flexible
16+
behavior in combination with `enumerate` A start parameter was also added like in `enumerate`
1417

1518
## Release 0.4.1
1619
Fix python 3 build error due to wheel installation of enum34. Package no longer depends on enum34

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Function | Description | Type
245245
`inits()` | Returns consecutive inits of sequence | transformation
246246
`tails()` | Returns consecutive tails of sequence | transformation
247247
`zip(other)` | Zips the sequence with `other` | transformation
248-
`zip_with_index()` | Zips the sequence with the index starting at zero on the left side | transformation
248+
`zip_with_index(start=0)` | Zips the sequence with the index starting at `start` on the right side | transformation
249249
`enumerate(start=0)` | Zips the sequence with the index starting at `start` on the left side | transformation
250250
`inner_join(other)` | Returns inner join of sequence with other. Must be a sequence of `(key, value)` pairs | transformation
251251
`outer_join(other)` | Returns outer join of sequence with other. Must be a sequence of `(key, value)` pairs | transformation

functional/pipeline.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,16 +1035,16 @@ def zip(self, sequence):
10351035
"""
10361036
return self._transform(transformations.zip_t(sequence))
10371037

1038-
def zip_with_index(self):
1038+
def zip_with_index(self, start=0):
10391039
"""
1040-
Zips the sequence to its index, with the index being the first element of each tuple.
1040+
Zips the sequence to its index, with the index being the second element of each tuple.
10411041
10421042
>>> seq(['a', 'b', 'c']).zip_with_index()
1043-
[(0, 'a'), (1, 'b'), (2, 'c')]
1043+
[('a', 0), ('b', 1), ('c', 2)]
10441044
10451045
:return: sequence zipped to its index
10461046
"""
1047-
return self._transform(transformations.zip_with_index_t())
1047+
return self._transform(transformations.zip_with_index_t(start))
10481048

10491049
def enumerate(self, start=0):
10501050
"""

functional/test/test_functional.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,14 @@ def test_zip(self):
636636

637637
def test_zip_with_index(self):
638638
l = [2, 3, 4]
639-
e = [(0, 2), (1, 3), (2, 4)]
639+
e = [(2, 0), (3, 1), (4, 2)]
640640
result = seq(l).zip_with_index()
641641
self.assertIteratorEqual(result, e)
642642
self.assert_type(result)
643+
e = [(2, 5), (3, 6), (4, 7)]
644+
result = seq(l).zip_with_index(5)
645+
self.assertIteratorEqual(result, e)
646+
self.assert_type(result)
643647

644648
def test_to_list(self):
645649
l = [1, 2, 3, "abc", {1: 2}, {1, 2, 3}]

functional/transformations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from __future__ import absolute_import
33

44
from functools import reduce, partial
5-
from itertools import dropwhile, takewhile, islice
5+
from itertools import dropwhile, takewhile, islice, count
66
import collections
77
import types
88

@@ -281,14 +281,14 @@ def zip_t(zip_sequence):
281281
)
282282

283283

284-
def zip_with_index_t():
284+
def zip_with_index_t(start):
285285
"""
286286
Transformation for Sequence.zip_with_index
287287
:return: transformation
288288
"""
289289
return Transformation(
290290
'zip_with_index',
291-
enumerate,
291+
lambda sequence: zip(sequence, count(start=start)),
292292
None
293293
)
294294

0 commit comments

Comments
 (0)