Python 3.x 如何提取以‘;s’;并以‘结束;p’;在PYTHON中使用filter()函数输入_列表?
使用Python 3.x 如何提取以‘;s’;并以‘结束;p’;在PYTHON中使用filter()函数输入_列表?,python-3.x,startswith,ends-with,filterfunction,Python 3.x,Startswith,Ends With,Filterfunction,使用filter功能提取input_list中以s开头、以p结尾的名称列表(s和p都是小写) 输出应为: input_list = ['soap','sharp','shy','silent','ship','summer','sheep'] 给你: ['soap', 'sharp', 'ship', 'sheep'] 这是如何容易做到的- list(filter(lambda x: x.startswith("s") and x.endswith("p"), input_list))
filter
功能提取input_list
中以s
开头、以p
结尾的名称列表(s和p都是小写)
输出应为:
input_list = ['soap','sharp','shy','silent','ship','summer','sheep']
给你:
['soap', 'sharp', 'ship', 'sheep']
这是如何容易做到的-
list(filter(lambda x: x.startswith("s") and x.endswith("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=列表(过滤器(λx:x[0]='s'和x[-1]='p',输入列表))
打印(sp)
sp=list(filter(lambda x:x[0:].startswith('s')和x[-1]。endswith('p'),input_list))打印(sp)您能解释一下吗?最好不要复制答案。这里已经有一行与此类似:第二行的另一行,有些人可能会觉得更可读一些:sp=[w代表输入列表中的w,如果w.startswith('s')和w.endswith('p')]
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)