1

I would like to know if this a syntax of Python or a numpy related thing.

For example:

x = numpy.array([(1, 2), (3, 4)])
print(x[:,0])
# [1, 3]

How can I implement my own method for this operation ? What is the name of ':,0' syntax ?

  • 3
    Does this answer your question? [Python Array Slice With Comma?](https://stackoverflow.com/questions/9972391/python-array-slice-with-comma) – luigigi Jan 15 '20 at 14:46
  • 2
    it basically means, all rows denoted by `:` and the 0-th column – luigigi Jan 15 '20 at 14:47
  • @luigigi In the link you posted, the answer mentions that "Note however that this syntax will only work for numpy arrays and not general python lists.". But how does numpy makes a use of a syntax python cannot handle ? I mean shouldn't python have a syntax that supports slices with comma ? –  Jan 15 '20 at 14:50
  • 2
    @luigigi Oh, it actually answers my question. It's a tuple object. Should have read a little bit further. –  Jan 15 '20 at 14:53
  • The python interpreter handles this syntax, but it's up to the object's methods to handle the result or not. `numpy` does, lists do not. In many contexts a comma produces a tuple. – hpaulj Jan 15 '20 at 16:32

0 Answers0