關于條件過濾
代碼可以這么寫嗎
def toUppers(L):
Y = []
for x in L if isinstance(x, str):
Y.append(x.upper())
? ? return Y
print toUppers(['Hello', 'world', 101])
應該怎么改?
代碼可以這么寫嗎
def toUppers(L):
Y = []
for x in L if isinstance(x, str):
Y.append(x.upper())
? ? return Y
print toUppers(['Hello', 'world', 101])
應該怎么改?
2017-07-23
舉報
2017-07-24
不可以,應該:
def toUppers(L):
? ? Y = []
? ? for x in L:
? ? ? ? if isinstance(x, str):
? ? ? ? ? ? Y.append(x.upper())
? ? return Y
print toUppers(['Hello', 'world', 101])