Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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_Loops_Url_Tuples - Fatal编程技术网

Python 从列表中提取某些元组

Python 从列表中提取某些元组,python,loops,url,tuples,Python,Loops,Url,Tuples,我不确定这是否是正确的方法,但我已经创建了一个元组列表,其中包含一个表单类型和相应的URL。我想隔离那些表单类型为10-K的元组,并尝试使用我在堆栈溢出中找到的代码。然而,我最终得到了一个没有内容的新列表,尽管我知道有几种10-K表单类型。 我是个初学者,所以请善待我 file = '10-K' req_urls = [] for tuple in typeandurls: if file in tuple: req_urls.append else:

我不确定这是否是正确的方法,但我已经创建了一个元组列表,其中包含一个表单类型和相应的URL。我想隔离那些表单类型为10-K的元组,并尝试使用我在堆栈溢出中找到的代码。然而,我最终得到了一个没有内容的新列表,尽管我知道有几种10-K表单类型。 我是个初学者,所以请善待我

file = '10-K'
req_urls = []

for tuple in typeandurls:
     if file in tuple:
           req_urls.append
     else:
           pass

另一种好方法是使用列表理解,如:

file = '10-K'
req_urls = [tup for tup in typeandurls if file in tup]
这里我假设
tuple中的file
是测试元素是否应属于输出集合的方式


这样做的好处是,您的代码乍一看更具可读性。您的意思是
req\u url.append(tuple)
?另外,请注意您不需要
else:pass
。这就像根本没有
其他的
一样。谢谢,这些评论都很有意义!