Get the first character of an empty python string:
>>> mystring = "hello" >>> print(mystring[0]) h >>> print(mystring[:1]) h >>> print(mystring[3]) l >>> print(mystring[-1]) o >>> print(mystring[2:3]) l >>> print(mystring[2:4]) ll
Get the first character from a string in the first position of a python list:
>>> myarray = [] >>> myarray.append("blah") >>> myarray[0][:1] 'b' >>> myarray[0][-1] 'h' >>> myarray[0][1:3] 'la'
Many people get here because they mix Python list object operators and Numpy ndarray object operators:
Numpy operations are very different from python list operations.
Wrap your head around two conflicting worlds of Python, “list slicing, indexing, subset,” and then “Masking, slicing, subset, indexing,” “Numping,” and then numpy Enhanced fancy indexing.
These two videos cleaned me up:
"Losing your cycles, fast numerical calculations with NumPy" from PyCon 2015: https://youtu.be/EEUXKG97YRw?t=22m22s
"Beginner for Beginners" SciPy 2016 Tutorial "by Alexander Chabot LeClerc: https://youtu.be/gtejJ3RCddE?t=1h24m54s
Eric Leschinski Oct 02 '15 at 18:37 2015-10-02 18:37
source share