input_list = ['soap','sharp','shy','silent','ship','summer','sheep']
使用s
提取p
中以input_list
开头并以filter
结尾({s和'p'均为小写))的名称列表功能。
输出应为:
['soap', 'sharp', 'ship', 'sheep']
答案 0 :(得分:1)
这是如何轻松完成-
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)
答案 1 :(得分:1)
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)
答案 2 :(得分:1)
sp = list(filter(lambda x: x.startswith("s") and x.endswith("p"), input_list))
print(sp)
这会给你正确的答案
答案 3 :(得分:0)
您在这里:
list(filter(lambda x: x.startswith("s") and x.endswith("p"), input_list))
答案 4 :(得分:0)
sp = list(filter(lambda x:x[0]=='s' and x[-1]=='p',input_list))
print(sp)
答案 5 :(得分:-1)
使用以下代码示例可以轻松完成:
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)