Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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 编译一个';如果';在“a”中;至于;同路_Python - Fatal编程技术网

Python 编译一个';如果';在“a”中;至于;同路

Python 编译一个';如果';在“a”中;至于;同路,python,Python,当然,只是为了好奇和知识。 你有什么建议把这两行代码合并成一行吗 我现在所拥有的: for files in os.listdir(path): if files.endswith("geneclusters.txt"): 我想要得到的是: for (files in os.listdir(path)) if files.endswith("geneclusters.txt"): 有什么建议吗?您可以使用列表理解: for files in [f for f in os.listdi

当然,只是为了好奇和知识。 你有什么建议把这两行代码合并成一行吗

我现在所拥有的:

for files in os.listdir(path):
    if files.endswith("geneclusters.txt"):
我想要得到的是:

for (files in os.listdir(path)) if files.endswith("geneclusters.txt"):

有什么建议吗?

您可以使用列表理解:

for files in [f for f in os.listdir(path) if f.endswith("geneclusters.txt")]:

您可以使用列表理解:

for files in [f for f in os.listdir(path) if f.endswith("geneclusters.txt")]:
当然(使用列表理解),取决于您下一步要做什么:

for file in [f for f in os.listdir(path) if f.endswith("geneclusters.txt")]:
当然(使用列表理解),取决于您下一步要做什么:

for file in [f for f in os.listdir(path) if f.endswith("geneclusters.txt")]:

不能将
if
语句合并为
for
语句。但是,您可以在
for
语句中使用生成器表达式

#            V --- generator expression ---------------------------------------------- V
for files in (files for files in os.listdir(path) if files.endswith("geneclusters.txt")):
    ...
请注意,没有性能优势,事实上,这通常较慢。最重要的是,它通常可读性较差


对于此类用例,不要使用由
[]
分隔的列表理解,而不是
()
。它在内存中创建整个中间列表。

不能将
if
语句合并为
for
语句。但是,您可以在
for
语句中使用生成器表达式

#            V --- generator expression ---------------------------------------------- V
for files in (files for files in os.listdir(path) if files.endswith("geneclusters.txt")):
    ...
请注意,没有性能优势,事实上,这通常较慢。最重要的是,它通常可读性较差


对于此类用例,不要使用由
[]
分隔的列表理解,而不是
()
。它会在内存中创建整个中间列表。

根据您在if之后要执行的操作,您可以使用列表理解有一种称为列表理解的方法,它可以在一行中创建列表,并可以添加过滤,如果这是您想要做的。要在“endswith”中打开特定文件,您可以在这里使用
列表理解
[对os.listdir(path)if files.endswith(“geneclusters.txt”)中的文件执行操作]
根据您下一步想做什么,至少建议使用生成器表达式,而不是列表理解。在这种情况下,后面的内存分配完全是浪费。根据您在if之后想做什么,您可以使用列表理解有一种称为列表理解的方法,它使用如果需要,可以添加筛选。要在“endswith”中打开特定文件,可以使用
列表理解
此处
[要为os.listdir(path)if files.endswith(“geneclusters.txt”)中的文件执行操作]
根据您下一步要做的事情,至少建议使用生成器表达式,而不是列表理解。在这种情况下,后面的内存分配完全是浪费。您缺少结尾的括号,但由于字符限制,我无法编辑。您缺少结尾的括号,但我无法编辑字符限制。