Python Array Slice With Comma?

前端 未结 3 1457
温柔的废话
温柔的废话 2020-11-27 05:25

I was wondering what the use of the comma was when slicing Python arrays - I have an example that appears to work, but the line that looks weird to me is

p =         


        
3条回答
  •  暖寄归人
    2020-11-27 05:39

    It slices with a tuple. What exactly the tuple means depends on the object being sliced. In NumPy arrays, it performs a m-dimensional slice on a n-dimensional array.

    >>> class C(object):
    ...   def __getitem__(self, val):
    ...     print val
    ... 
    >>> c = C()
    >>> c[1:2,3:4]
    (slice(1, 2, None), slice(3, 4, None))
    >>> c[5:6,7]
    (slice(5, 6, None), 7)
    

提交回复
热议问题