Discuss / Python / 今天的作业仔细分析了一下.慢慢懂了.

今天的作业仔细分析了一下.慢慢懂了.

Topic source

-- coding: utf-8 --

展开正则表达式用循环分析:

L1 = ['Hello', 'World', 18, 'Apple', None] L2 = []

for x in L1 : if isinstance(x , str): L2.append(x) else: pass print(L2) print([s.lower() for s in L2])

所以正则表达式结果:

print([s.lower() for s in L1 if isinstance(s,str)])

这里如果是False则默认PASS.

关于s.lower() for s in L2 循环表现为: s.lower() for s in L2: L3.append(s) 假定L3是一个空List.

补充一下,在原题中已经有了print(L2) 所以在答题上填: L2 = []

for x in L1 : if isinstance(x , str): L2.append(x) else: pass

L2 = [s.lower() for s in L2] 即可

L2 = [s.lower() for s in L1 if isinstance(s, str)] 上述是正确答案


  • 1

Reply