练习
1、- 使用列表推导式找出单词长度大于n的单词
ls = ["google","apple","hello","battle"]
n = 5
for item in ls:
if len(item) > n:
print(item)
google
battle
2、- 使用列表推导式寻找两个列表中的相同元素
ls1 = ["google","apple","hello","battle"]
ls2 = ["gooogle","dpple","hello","baattle"]
for item1 in ls1:
for item2 in ls2:
if item1 == item2 :
print(item1)
hello
ls1 = ["google","apple","hello","battle"]
ls2 = ["gooogle","dpple","hello","baattle"]
[a for a in ls1 for b in ls2 if a == b]
['hello']
3、- 去除一个列表中相领且重复的元素。
ls =