Python: single colon vs double colon

前端 未结 5 1320
醉梦人生
醉梦人生 2020-12-31 15:05

What is the difference between single and double colon in this situation? data[0:,4] vs data[0::,4]

women_only_stats = data[0::,4]         


        
5条回答
  •  轮回少年
    2020-12-31 15:21

    Both syntaxes result in the same indexes.

    class Foo(object):
      def __getitem__(self, idx):
        print(idx)
    
    Foo()[1::,6]
    # prints (slice(1, None, None), 6)
    Foo()[1:,6]
    # prints (slice(1, None, None), 6)
    

    Basically, 1::,6 is a tuple of a slice (1::) and a number (6). The slice is of the form start:stop[:stride]. Leaving the stride blank (1::) or not stating it (1:) is equivalent.

提交回复
热议问题