我无法理解这个双if和else语句
完整代码如下,无法理解的是
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
答案 0 :(得分:0)
如果添加括号,也许更容易理解:
((SELECT TOP 100 PERCENT * FROM
(SELECT TOP 100 PERCENT tdate, parti,
ABS(SUM(amtdr)) AS Dr, SUM(amtcr) AS Cr, nart FROM
dbo.dbo_dayb a
WHERE (account = 'bank')
GROUP BY idno, tdate, parti, nart)
UNION ALL
(SELECT tdate, parti, amtdr, amtcr, nart
FROM dbo.dbo_dayb
WHERE (account = N'PDC account')
AND (post = N'Cr')) DERIVEDTBL) )
ORDER BY tdate
如果满足条件,则括号之间的部分计算结果为0,否则计算结果为1。
答案 1 :(得分:0)
我试着在没有代码的情况下解释....
<div class="demo">
<input type="text" id="phone_world" onChange = "hello()" value="" size="25">
<label for="phone_world" id="descr_world">Страны мира</label>
答案 2 :(得分:-1)
label[j]=0 if imgs[i].split('.')[0]=='cat' else 1
等于:
if imgs[i].split('.')[0]=='cat':
label[j]= 0
else:
label[j]= 1
答案 3 :(得分:-1)
如果你知道c,你可以将它与三元运算符联系起来:
label[j] = strcmp(splitted_imgs[0], "cat") ? 0: 1;
或在python中,等效代码(如kingguy所述):
if imgs[i].split('.')[0] == 'cat':
label[j] = 0
else:
label[j] = 1
但是因为你正在使用python,所以总是欣赏pythonic(正如你所使用的那样):
label[j] = 0 if imgs[i].split('.')[0] == 'cat' else 1
答案 4 :(得分:-1)
它被称为ternary operator。 该陈述等于:
if imgs[i].split('.')[0] == 'cat':
label[j] = 0
else:
label[j] = 1
在Wikipedia中,据说:
一般形式(在Python中)是:
result = x if a > b else y