將列表中的大寫字母轉換成小寫
如果list中既包含字符串,又包含整數,由於非字符串類型沒有lower()方法,
L1 = ['Hello', 'World', 18, 'Apple', None]
請修改列表生成式,通過添加if語句保證列表生成式能正確地執行.
# -*- coding: utf-8 -*- L1 = ['Hello', 'World', 18, 'Apple', None] L2 = [s.lower() for s in L1 if isinstance(s,str)==True] # 測試: print(L2) if L2 == ['hello', 'world', 'apple']: print('測試通過!') else: print('測試失敗!')