1
input_list =  ['soap','sharp','shy','silent','ship','summer','sheep']

在using函数中提取以 an 开头s并以 a 结尾的名称列表p('s' 和 'p' 都是小写字母) 。input_listfilter

输出应该是:

['soap', 'sharp', 'ship', 'sheep']
4

7 回答 7

1

sp = list(filter(lambda x: x.startswith("s") and x.endswith("p"), input_list))

print(sp)

这会给你正确的答案

于 2021-07-12T06:46:45.150 回答
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)
于 2020-04-27T16:33:57.557 回答
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)
于 2021-06-01T13:37:01.907 回答
0

干得好:

list(filter(lambda x: x.startswith("s") and x.endswith("p"), input_list))
于 2020-04-27T16:22:08.657 回答
0
sp = list(filter(lambda word: (word[0]=="s") and (word[-1]=="p"), input_list))
于 2022-01-06T18:18:12.517 回答
0
sp = list(filter(lambda x:x[0]=='s' and x[-1]=='p',input_list))


print(sp)
于 2021-02-16T19:58:02.867 回答
-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) 
于 2021-05-15T05:47:02.237 回答