Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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中使用fnmatch.filter来过滤字符串列表是否正确?_Python_String - Fatal编程技术网

在python中使用fnmatch.filter来过滤字符串列表是否正确?

在python中使用fnmatch.filter来过滤字符串列表是否正确?,python,string,Python,String,我有变量类别中的URL列表,我想过滤该列表以获取存在http的URL //categories contains list of urls filtered = set(fnmatch.filter(categories,'http*')) 这是可行的,但是set(如果c.startswith('http'),那么c代表类别中的c)更直接,因为它在将列表转换为一个集合之前不会构建过滤列表。谢谢Dan!但哪种方法最有效?因为我们在这里使用for循环,我有10000个字符串要解析,这需要2.3毫秒

我有变量
类别中的URL列表
,我想过滤该列表以获取存在
http
的URL

//categories contains list of urls
filtered = set(fnmatch.filter(categories,'http*'))

这是可行的,但是
set(如果c.startswith('http'),那么c代表类别中的c)
更直接,因为它在将列表转换为一个集合之前不会构建过滤列表。

谢谢Dan!但哪种方法最有效?因为我们在这里使用for循环,我有10000个字符串要解析,这需要2.3毫秒。设置过滤器需要5.56毫秒。对于10000个
'http'
sOkay的列表。再次感谢:)谢谢你格式化代码。我尊重你们的观点,但这是我第一次讨论堆栈溢出问题,他们已经阻止我提问了。我是python新手,我问的问题是在阅读了这些建议并进行了很长时间的研究之后。别紧张,伙计们!