0

If we create an array with Numpy , we can use many functionalities given by numpy library.

For example if c is a matrix

print(c[:,1])

will print every value in the column 1.

Now, when i index the c matrix in this way, am i indexing using a tuple ? If yes, how is possible to have a tuple with ':' inside ?

3
  • Are you asking how slicing works or how to have the ':' character in a tuple? Commented Jan 31, 2018 at 12:59
  • print(c[EMPTY:EMPTY,1]) is better understood. Commented Jan 31, 2018 at 13:02
  • @doctorlove: I think Tantaros wants to understand how Numpy processes such slices internally. Commented Jan 31, 2018 at 13:03

1 Answer 1

2

The colon syntax is syntactical sugar for a slice(..) object. Your expression is equvalent to:

#        v slice object
print(c[(slice(None), 1)])
#       ^   tuple      ^

So you have passed a tuple containing a slice(None) object as first element, and 1 as second element.

The mapping of slice syntax to slice(..) objects is as follows:

  1. the colon : is equivalent to slice(None);
  2. if it is :b, then it is equivalent to slice(b);
  3. a: is equivalent to slice(a, None);
  4. a:b is equivalent to slice(a, b);
  5. ::c is equivalent to slice(None, None, c);
  6. :b:c to slice(None, b, c);
  7. a::c is equivalent to slice(a, None, c); and
  8. a:b:c to slice(a, b, c).

Note that slice syntax is only supported in the context of an itemgetter (so x[..]).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.