This question already has answers here:
Python: single colon vs double colon
                                
                                    (5个答案)
                                
                        
                                3年前关闭。
            
                    
我正在尝试遵循Kaggle Titanic tutorial使用Python和NumPy解决问题。我很难理解data [0 ::,]和data [0 :,]有什么区别。我复制粘贴下面的相关代码片段:

for i in xrange(number_of_classes):       #loop through each class
    for j in xrange(number_of_price_brackets):   #loop through each price bin

        women_only_stats = data[                          # Which element
                                (data[0::, 4] == "female") &   # is a female and
                                (data[0::, 2].astype(np.float) # was ith class
                                  == i+1)
                                &                              # and
                                (data[0:, 9].astype(np.float)  # was greater
                                  >= j * fare_bracket_size)    # than this bin
                                &                              # and
                               (data[0:, 9].astype(np.float)   # less than
                                  < (j+1)*fare_bracket_size)   # the next bin
                               , 1]                        # in the 2nd col

最佳答案

没什么区别,这两种方法都将以相同的方式挂接到__getitem__中。

>>> class Thing(object):
...     def __getitem__(self, item):
...         print(repr(item))
...
>>> t = Thing()
>>> t[0:, 4]
(slice(0, None, None), 4)
>>> t[0::, 4]
(slice(0, None, None), 4)

关于python - 过滤numpy数组时0::和0:有什么区别? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38223274/

10-10 06:49