input_list = ['soap','sharp','shy','silent','ship','summer','sheep']
在using函数中提取以 an 开头s
并以 a 结尾的名称列表p
('s' 和 'p' 都是小写字母) 。input_list
filter
输出应该是:
['soap', 'sharp', 'ship', 'sheep']
input_list = ['soap','sharp','shy','silent','ship','summer','sheep']
在using函数中提取以 an 开头s
并以 a 结尾的名称列表p
('s' 和 'p' 都是小写字母) 。input_list
filter
输出应该是:
['soap', 'sharp', 'ship', 'sheep']
sp = list(filter(lambda x: x.startswith("s") and x.endswith("p"), input_list))
print(sp)
这会给你正确的答案
在这里如何轻松完成-
input_list = ['soap','sharp','shy','silent','ship','summer','sheep']
def fil_func(name):
if name[0]=='s' and name[-1]=='p':
return True
correct_name = []
for i in input_list:
name = list(filter(fil_func, input_list)) # list() is added because filter function returns a generator.
print(name)
input_list = ['soap', 'sharp', 'shy', 'silent', 'ship', 'summer', 'sheep']
sp = list(filter(lambda x: x[0] == 's' and x[-1]=='p', input_list))
print(sp)
干得好:
list(filter(lambda x: x.startswith("s") and x.endswith("p"), input_list))
sp = list(filter(lambda word: (word[0]=="s") and (word[-1]=="p"), input_list))
sp = list(filter(lambda x:x[0]=='s' and x[-1]=='p',input_list))
print(sp)
可以使用以下代码示例轻松完成:
input_list = ['soap','sharp','shy','silent','ship','summer','sheep']
sp = list(filter(lambda x:x[0]=='s' and x[-1]=='p', input_list))
print(sp)