变量&;的Python搜索列表;返回匹配元素
我试图在python中提供一个函数,该函数将根据用户输入搜索列表,并返回该列表中所有匹配的行 例如,如果用户键入hello,它将返回该列表中出现的任何hello 到目前为止,我有:变量&;的Python搜索列表;返回匹配元素,python,list,search,match,Python,List,Search,Match,我试图在python中提供一个函数,该函数将根据用户输入搜索列表,并返回该列表中所有匹配的行 例如,如果用户键入hello,它将返回该列表中出现的任何hello 到目前为止,我有: if(prefixes in list): matching = [s for s in list if prefixes in s] for release in matching: name = release.lstrip('rd').rstrip('xm
if(prefixes in list):
matching = [s for s in list if prefixes in s]
for release in matching:
name = release.lstrip('rd').rstrip('xml').lstrip('-').rstrip('.')
path = rdDirectory+release
print '%-40s %s' % (name, path)
我也是新来的,所以任何帮助都会被感激地接受
输入=:
./tpupdate.py -l asterix where asterix = search variable
输出=
Available Releases Path
asterix-1.0.0-production
zebedee-3.2.1-production
等等
问题是:它不会根据putted prefix中的用户返回,而是返回列表的所有元素您的列表是什么样子的?你能展示一个输入和输出的示例吗?你给了我们一些代码,但你没有说它是如何工作的。你期望它做什么?它实际上做什么?如果输出错误,请举例说明正确的输出。如果出现错误,请将其与堆栈跟踪一起完整发布。
rstrip()
和lstrip()
不要删除完整字符串。它们将删除名为的每个字符.rstrip('xml')
并不是指“从末尾删除字符串'xml'
”,而是指“从末尾删除字符'x'
、'm'
和'l'
”<代码>某些带有lxmmmlx的句子将被删除。显示YOUR打印版本
输出