#!/usr/bin/env python3
# -*- coding: utf-8 -*-
L1 = ['Hello', 'World',18,'Apple',None]
L2=[]
for s in L1:
if isinstance(s,str):
L2.append(s.lower())
print(L2)
把上面的程序简化,如下:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
L1 = ['Hello', 'World',18,'Apple',None]
L2=[n.lower() for n in [s for s in L1 if isinstance(s,str)]]
print(L2)
loveprruy
把上面的程序简化,如下: