Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
变量&;的Python搜索列表;返回匹配元素_Python_List_Search_Match - Fatal编程技术网

变量&;的Python搜索列表;返回匹配元素

变量&;的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

我试图在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('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
打印版本
输出