作业,两种方法
Topic source# 自问自答,(笑.jpg)
# 使用 filter函数将None批量移除
L2 = list(filter(None,[x.lower() if isinstance(x,Iterable) else None for x in List1]))
- 1
# 自问自答,(笑.jpg)
# 使用 filter函数将None批量移除
L2 = list(filter(None,[x.lower() if isinstance(x,Iterable) else None for x in List1]))
独孤伶俜233
List1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [x.lower() for x in List1 if isinstance(x, Iterable)]
# 测试:
print(L2)
if L2 == ['hello', 'world', 'apple']:
print('测试通过!')
else:
print('测试失败!')
# 第二种办法用了很傻很笨的办法,有木有大神优化一下下呢~
L2 = [x.lower() if isinstance(x,Iterable) else None for x in List1] # 把非可迭代对象生成None
L3 = [x for x, v in enumerate(L2) if v == None] # 找出None的下标
L3.reverse() # 反向排序,防止pop时pop的不对
for i in L3 :
L2.pop(i)
# 测试:
print(L2)
if L2 == ['hello', 'world', 'apple']:
print('测试通过!')
else:
print('测试失败!')