Skip to content

Instantly share code, notes, and snippets.

@ChillarAnand
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save ChillarAnand/2abf4ecc6066bbc6b22a to your computer and use it in GitHub Desktop.

Select an option

Save ChillarAnand/2abf4ecc6066bbc6b22a to your computer and use it in GitHub Desktop.
a slice of python
In [118]: alpha
Out[118]: ['a', 'b', 'c', 'd', 'e', 'f']
In [119]: alpha[20]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-119-c5b145def39d> in <module>()
----> 1 alpha[20]
IndexError: list index out of range
In [120]: alpha[20:]
Out[120]: []
In [122]: alpha = ['a', 'b', 'c', 'd', 'e', 'f']
In [123]: alpha
Out[123]: ['a', 'b', 'c', 'd', 'e', 'f']
In [124]: alpha[0]
Out[124]: 'a'
In [127]: alpha[0] = 'A'
In [128]: alpha
Out[128]: ['A', 'b', 'c', 'd', 'e', 'f']
In [129]: alpha[0,1]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-129-c7eb16585371> in <module>()
----> 1 alpha[0,1]
TypeError: list indices must be integers, not tuple
In [130]: alpha[0:1]
Out[130]: ['A']
In [131]: alpha[0:1] = 'a'
In [132]: alpha
Out[132]: ['a', 'b', 'c', 'd', 'e', 'f']
In [133]: alpha[0:2] = ['A', 'B']
In [134]: alpha
Out[134]: ['A', 'B', 'c', 'd', 'e', 'f']
In [135]: alpha[2:2] = ['x', 'xx']
In [136]: alpha
Out[136]: ['A', 'B', 'x', 'xx', 'c', 'd', 'e', 'f']
In [137]: alpha = ['a', 'b', 'c', 'd', 'e', 'f']
In [142]: alpha[1:5:2]
Out[142]: ['b', 'd']
In [143]: alpha[-1:-5:-2]
Out[143]: ['f', 'd']
In [144]: alpha[1:5:-2]
Out[144]: []
In [145]: alpha[-1:-5:2]
Out[145]: []
def py_slice_get_indices_ex(obj, start=None, stop=None, step=None):
length = len(obj)
if step is None:
step = 1
if step == 0:
raise Exception("Step cannot be zero.")
if start is None:
start = 0 if step > 0 else length - 1
else:
if start < 0:
start += length
if start < 0:
start = 0 if step > 0 else -1
if start >= length:
start = length if step > 0 else length - 1
if stop is None:
stop = length if step > 0 else -1
else:
if stop < 0:
stop += length
if stop < 0:
stop = 0 if step > 0 else -1
if stop >= length:
stop = length if step > 0 else length - 1
if (step < 0 and stop >= start) or (step > 0 and start >= stop):
slice_length = 0
elif step < 0:
slice_length = (stop - start + 1)/(step) + 1
else:
slice_length = (stop - start - 1)/(step) + 1
return (start, stop, step, slice_length)
In [21]: alpha = ['a', 'b', 'c', 'd', 'e', 'f']
In [22]: s = slice(None, None, None)
In [23]: s
Out[23]: slice(None, None, None)
In [24]: s.indices(len(alpha))
Out[24]: (0, 6, 1)
In [25]: range(*s.indices(len(alpha)))
Out[25]: [0, 1, 2, 3, 4, 5]
In [26]: s = slice(None, None, -1)
In [27]: range(*s.indices(len(alpha)))
Out[27]: [5, 4, 3, 2, 1, 0]
In [28]: s = slice(None, 3, -1)
In [29]: range(*s.indices(len(alpha)))
Out[29]: [5, 4]
In [170]: alpha
Out[170]: ['a', 'b', 'c', 'd', 'e', 'f']
In [171]: alpha[]
File "<ipython-input-171-a17d5cfb2c2f>", line 1
alpha[]
^
SyntaxError: invalid syntax
In [172]: alpha[:]
Out[172]: ['a', 'b', 'c', 'd', 'e', 'f']
In [173]: alpha[::]
Out[173]: ['a', 'b', 'c', 'd', 'e', 'f']
In [174]: alpha[:-1:]
Out[174]: ['a', 'b', 'c', 'd', 'e']
In [175]: alpha[:2:-1]
Out[175]: ['f', 'e', 'd']
In [176]: alpha[:10000000000000000000000000000000000000000000000000000000]
Out[176]: ['a', 'b', 'c', 'd', 'e', 'f']
In [177]: alpha[::1000000000]
Out[177]: ['a']
In [178]: alpha[::-1]
Out[178]: ['f', 'e', 'd', 'c', 'b', 'a']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment