0

I have been given, by someone else, a line of code that I am having trouble understanding.

inputString = "[1,2,3,4,5,6,7,8,9,10,11,12]"

a = inputString[1:-1].split(',')
z = zip(a[::2],a[1::2]) # this line
print a
print z

I understand the split function and how that is operating. I even understand the zip function. What I don't understand is what a[::2],a(1::2) is doing. I have tried modifying these bits of code and get varying results, but nothing that allows me to comprehend what modifying the code does. I am fairly new at Python, and could use a little help.

Typical, unmodified result from the code looks like this:

['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
[('1', '2'), ('3', '4'), ('5', '6'), ('7', '8'), ('9', '10'), ('11', '12')]

I am trying to get the result of 'a' to split into groups of 4 instead of groups of two, ie:

('1','2','3','4'),('5','6','7','8')....etc.
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
SnapperTrx
  • 23
  • 3
  • please see explanation here: http://stackoverflow.com/questions/509211/pythons-slice-notation – m.wasowski Jun 04 '14 at 21:45
  • You should really look into the [`itertools` recipe `grouper`](https://docs.python.org/2/library/itertools.html#recipes)... – jonrsharpe Jun 04 '14 at 21:47

2 Answers2

2
z = zip(a[::2],a[1::2])

The above line twice uses Explain Python's slice notation to step through list a. a[::2] is telling Python to get every other item in a. Below is a demonstration:

>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> lst[::2]  # Every other item (every item counting by 2's)
[1, 3, 5, 7, 9]
>>>

a[1::2] is telling Python to get every other item in a starting at index 1. Below is a demonstration:

>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> lst[1::2]  # Remember that Python indexes start at 0
[2, 4, 6, 8, 10]
>>>

zip is then pairing-up the items in the lists returned by a[::2] and a[1::2]:

>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> zip(lst[::2], lst[1::2])
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
>>>

To get the four-item tuples in your example, you can use the following:

>>> inputString = "[1,2,3,4,5,6,7,8,9,10,11,12]"
>>> a = inputString[1:-1].split(',')
>>> zip(*[iter(a)]*4)
[('1', '2', '3', '4'), ('5', '6', '7', '8'), ('9', '10', '11', '12')]
>>>

You might also want to read: How does zip(*[iter(s)]*n) work in Python?

Community
  • 1
  • 1
  • Just to make sure I understand correctly. In the list of numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], the number '1' is considered index position '0'? – SnapperTrx Jun 04 '14 at 21:53
  • @SnapperTrx - Yes. Python indexes are like C/C++ indexes: they start at index `0`. –  Jun 04 '14 at 21:56
  • Instead of zip, you can also use list comprehension and enumerate, like so: print [(x,y) for x, y in enumerate(s) if x%2!=0] – Stefan Gruenwald Jan 11 '16 at 06:40
0

If you want 4-tuples then you need to use 4 arguments and a stride of 4.

z = zip(a[::4], a[1::4], a[2::4], a[3::4])
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358