简体   繁体   English

用pythonic方式写一个复杂的循环

[英]Write a complex loop in pythonic way

I have the following Code 我有以下代码

import numpy as np
from readers import readerXYZ
import glob 
folder=r'N:\FolderXYZ/*'
D_list=[]
ctr=0

for name in glob.glob (folder):

    Zdata = readerXYZ(name, output_matrix=True) 

    #I Need this counter of the NaNs for future computations
    ctr=ctr+np.count_nonzero(np.isnan(Zdata))    
    a=list(Zdata.shape)
    D_list.append(a)

The program reads from different files stored in the Folder called "FolderXYZ", this is done with an external program called readerXYZ, what I want to store in the lis D_list is the dimensions of each Zdata Matrix, and at the same time count how many NaNs are in total. 该程序从存储在名为“FolderXYZ”的文件夹中的不同文件中读取,这是通过名为readerXYZ的外部程序完成的,我想要存储在lis D_list中的是每个Zdata矩阵的维度,同时计算多少NaNs总计。 This code works fine but takes so Long, how can I improve it? 这段代码工作正常,但需要很长时间,我该如何改进呢? thank you 谢谢

If time is your concern. 如果时间关注你。 You can consider processing the list in parallel. 您可以考虑并行处理列表。 Here is a reference for multiprocessing Python how to parallelize loops 以下是多处理Python如何并行化循环的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM