-1

I cant understand this double if and else statement

full code is below and where is cant understand is


if not imgs[i].startswith('.'):
                label[j]=0 if imgs[i].split('.')[0]=='cat' else 1

def LoadDirImgList(sampleNum,path): 
    data = np.empty((sampleNum,3,32,32),dtype="float32")
    label = np.empty((sampleNum,),dtype ="uint8")
    imgs = os.listdir(path)
    num = len(imgs)
    j=0
    for i in range(num):
        if not imgs[i].startswith('.'):
            label[j]=0 if imgs[i].split('.')[0]=='cat' else 1
            img = Image.open(path+imgs[i])
            arr = np.asarray (img, dtype ="float32")
            data [j,:,:,:] = [arr[:,:,0],arr[:,:, 1],arr[:,:, 2]] 
            j=j+1
    return data, label
melpomene
  • 79,257
  • 6
  • 70
  • 127
SwarmRL
  • 7
  • 1

5 Answers5

0

Perhaps it is easier to understand for you if you add parenthesis:

if not imgs[i].startswith('.'):
            label[j]= ( 0 if imgs[i].split('.')[0]=='cat' else 1 )

The part in between parenthesis evaluates to 0 if the condition is met and otherwise evaluates to 1.

FlorianK
  • 320
  • 2
  • 10
0

I'll try to explain without code....

If the snow cones are red, white or green play the song at the bottom
        If they are not red, white or green play Happy Birthday Song 
Play the National Anthem
Fixitrod
  • 125
  • 2
  • 10
-1
label[j]=0 if imgs[i].split('.')[0]=='cat' else 1

equals:

if imgs[i].split('.')[0]=='cat':
    label[j]= 0
else:
    label[j]= 1
jack-nie
  • 726
  • 6
  • 19
-1

If you know c you can relate this to the ternary operator as:

label[j] = strcmp(splitted_imgs[0], "cat") ? 0: 1;

or in python an equivalent code would be(as mentioned by kingguy):

if imgs[i].split('.')[0] == 'cat':
    label[j] = 0
else:
    label[j] = 1

but since you are using python, a pythonic is always appreciated(as you are using):

label[j] = 0 if imgs[i].split('.')[0] == 'cat' else 1
wolframalpha
  • 945
  • 2
  • 8
  • 22
-1

It is called ternary operator. The statement equals:

if imgs[i].split('.')[0] == 'cat':
    label[j] = 0
else:
    label[j] = 1

In Wikipedia, it is said:

The general form (in Python) is:

result = x if a > b else y
pjpj
  • 443
  • 1
  • 4
  • 20
  • Nope, not the same. In OP's example, if `label` does not exist before, it won't exist afterwards. – Jacob Vlijm Sep 04 '16 at 09:32
  • @JacobVlijm what do you mean by "label does not exist before"? – pjpj Sep 04 '16 at 09:38
  • The question is about the top two lines. If label was not defined earlier, it stil does not exist afterwards, if not `imgs[i].startswith('.')`. e.g. `print(label)` will raise `NameError`. – Jacob Vlijm Sep 04 '16 at 09:41
  • @JacobVlijm If `if not imgs[i].startswith('.'):` is true, then `if imgs[i].split('.')[0] == 'cat': label[j] = 0 else: label[j] = 1`, isn't that? – pjpj Sep 04 '16 at 09:46
  • Yes, but if not, label *does not exist afterwards* if not defined before. – Jacob Vlijm Sep 04 '16 at 09:55
  • @JacobVlijm Then what is `label = np.empty((sampleNum,),dtype ="uint8")`?? – pjpj Sep 04 '16 at 10:25
  • Hi pwd, the question is syntatically, not coincidentally how it is in *this* context, but even *if* label exists, the meaning is quite different. The code in his example *leave label as it is* `if not imgs[i].startswith('.')`, *whatever that is*. In your snippet, it becomes either `0` or `1`. – Jacob Vlijm Sep 04 '16 at 10:48
  • 1
    @JacobVlijm Okay, I finally got it, but I just ''translated" the statement and didn't care about the name error. – pjpj Sep 04 '16 at 11:00