列表生成式的使用

输入:['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)

猜你喜欢

转载自www.cnblogs.com/Tristan-Adams/p/9951591.html
今日推荐