Discuss / Python / 列表生成式作业

列表生成式作业

Topic source

LingByron

#1 Created at ... [Delete] [Delete and Lock User]

NO.1

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

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

NO.2

#!/usr/bin/env python3
# -*- coding:utf-8 -*-


L1 = ['Hello','World',18,'Apple',None]
L2 = []
for x in L1:
    if isinstance(x,str):
        L2.append(x.lower())
    else:
        continue
print(L2)

python3 homework.py

['Hello','World','Apple']

  • 1

Reply