将列表中的大写字母转换成小写如果list中既包含字符串,又包含整数,由于非字符串类型没有lower()方法,L1 = ['Hello', 'World', 18, 'Apple', None]请修改列表生成式,通过添加if语句保证列表生成式能正确地执行.
python;gutter:true;
-<em>- coding: utf-8 -</em>-</p>
<p>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('测试失败!')</p>
<pre><code>
</code></pre>
<p>
Original: https://www.cnblogs.com/chling/p/11763250.html
Author: it_逗逗
Title: python练习题:将列表中的大写字母转换成小写
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/712096/
转载文章受原作者版权保护。转载请注明原作者出处!