Question about array index and printing

username: Yolkandwhite
cooked: <p>I have question about array index</p>
<pre><code class="lang-auto">        mriidx = self.mriids[idx] #img file name
        maskidx = self.maskids[idx] #mask file name
        
        mask_file = os.path.join(self.masks_dir, maskidx)
        img_file = os.path.join(self.imgs_dir, mriidx)
        
        img = Image.open(img_file).convert("RGB")
        mask = Image.open(mask_file).convert("L")
                
        mask = np.array(mask)
        img = np.array(img)
        
        print("img shape :", img.shape,'\n', "mask shape :", mask.shape)

        obj_ids = np.unique(mask)
        
        print('obj_ids :',obj_ids)
        
        obj_ids = obj_ids[1:]
        
        print('obj_ids[1:] shape :', obj_ids[1:].shape)
        print('obj_ids[1:] :', obj_ids[1:])
</code></pre>
<p>and here are the two types of outputs I got</p>
<pre><code class="lang-auto">img shape : (256, 256, 3) 
 mask shape : (256, 256)
obj_ids : [0]
obj_ids[1:] shape : (0,)
obj_ids[1:] : []
</code></pre>
<pre><code class="lang-auto">img shape : (256, 256, 3) 
 mask shape : (256, 256)
obj_ids : [  0 255]
obj_ids[1:] shape : (0,)
obj_ids[1:] : []
</code></pre>
<p>My first question is in the first output, obj_ids is [0].<br>
then how is obj_ids[1:] not have error? isn’t it out of index range?</p>
<p>and my second question is in the second output.<br>
why does obj_ids has additional space?<br>
doesn’t it has to be [0 255] not [    0 255]?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Yolkandwhite" data-post="1" data-topic="74079">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yolkandwhite/48/17604_2.png" class="avatar"> Yolkandwhite:</div>
<blockquote>
<p>My first question is in the first output, obj_ids is [0].<br>
then how is obj_ids[1:] not have error? isn’t it out of index range?</p>
</blockquote>
</aside>
<p>No, as you will get an empty tensor, if the slice is invalid (same as in numpy):</p>
<pre data-code-wrap="python"><code class="lang-python">x = torch.randn(10)
n = np.random.randn(10)

print(x[11:])
&gt; tensor([])
print(n[11:])
&gt; []
</code></pre>
<p>However, direct indexing will yield an error.</p>
<aside class="quote no-group quote-modified" data-username="Yolkandwhite" data-post="1" data-topic="74079">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yolkandwhite/48/17604_2.png" class="avatar"> Yolkandwhite:</div>
<blockquote>
<p>why does obj_ids has additional space?<br>
doesn’t it has to be [0 255] not [ 0 255]?</p>
</blockquote>
</aside>
<p>Numpy formats the output like this (PyTorch as well, but with a comma <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"> ).</p>

username: Yolkandwhite
cooked: <p>in the second question I was talking about<br>
obj_ids output having additional double space in front of 0.<br>
not the way it shows its output.</p>
<p>in the first output<br>
obj_ids : [0]<br>
it doen’t have additional double space</p>

username: ptrblck
cooked: <p>I still think it is the desired print format to align multiple rows by the value:</p>
<pre><code class="lang-python">print(torch.tensor([0, 255]))
&gt; tensor([  0, 255])
print(torch.tensor([0, 1000]))
&gt; tensor([   0, 1000])
print(torch.tensor([[0, 10000000], [1, 1]]))
&gt; tensor([[       0, 10000000],
          [       1,        1]])
</code></pre>

username: Yolkandwhite
cooked: <p>Oh… It follows its length of largest value…<br>
I didn’t realized that.<br>
Thank you very much!</p>

username: Yolkandwhite
cooked: <p>then could you tell me how <strong>obj_ids[1:]</strong> in the second output is not 255?<br>
and how is the shape not (1, )?</p>

username: ptrblck
cooked: <p>You are reassigning and then slicing again the object:</p>
<pre><code class="lang-python">obj_ids = obj_ids[1:] # here it will only have a single element
        
print('obj_ids[1:] shape :', obj_ids[1:].shape)
print('obj_ids[1:] :', obj_ids[1:]) # is empty now
</code></pre>