第二個(gè)if為啥不能用and
def toUppers(L):
? ? return [x.upper() for x in L and isinstance(x,str)]
print toUppers(['Hello', 'world', 101])
def toUppers(L):
? ? return [x.upper() for x in L and isinstance(x,str)]
print toUppers(['Hello', 'world', 101])
2016-02-18
舉報(bào)
2016-02-18
這一段最先被執(zhí)行的部分是 ?L and isinstance(x,str)?
然后再對(duì)這個(gè)表達(dá)式的值迭代(雖然這個(gè)式子返回布爾值并不能迭代,不過(guò)這不是重點(diǎn))
重點(diǎn)是x得有值才能被引用,然而在迭代了之后x才被賦值,并且執(zhí)行到isinstance(x,str)時(shí)迭代還沒(méi)開(kāi)始
所以你的寫(xiě)法有問(wèn)題
2016-02-18
for循環(huán)是迭代不是條件,and用于兩個(gè)條件之間