列表生成式的使用

输入:['Hello', 'World', 18, 'Apple', None]

输出:['hello', 'world', 'apple']

L = ['Hello', 'World', 18, 'Apple', None]
print([w.lower() for w in L if isinstance(w, str)])

# -- coding: utf-8 --
L = ['Hello', 'World', 18, 'Apple', None]
L2 = []
L2 = [w.lower() for w in L if isinstance(w, str)]
print(L2)

L = ['Hello', 'World', 18, 'Apple', None]
i = 0
L2 = []
while i < len(L):
    if isinstance(L[i], str) and i < len(L):
        L2.append(L[i])
    i = i + 1
print(L2)


posted @ 2018-11-13 12:58  崔杯杯  阅读(246)  评论(0编辑  收藏  举报