简体   繁体   English

包含矩阵和向量的动态列表/数组

[英]A dynamic list/array which contains matrices and vectors

so I have the following setup. 所以我有以下设置。 I have a folder with two different file types. 我有一个包含两种不同文件类型的文件夹。 One file type (.m) are files of matrices and the other file type are the corresponding vectors (.txt). 一种文件类型(.m)是矩阵文件,另一种文件类型是相应的向量(.txt)。 They have the same name ie matrix1.m and matrix1.txt 它们具有相同的名称,即matrix1.m和matrix1.txt

I want to create a list or an array such that I can save each matrix and each corresponding vector. 我想创建一个列表或数组,以便可以保存每个矩阵和每个对应的向量。 I thought about a panda data frame, but I do not know if this is useful here. 我想到了一个熊猫数据框,但是我不知道这在这里是否有用。

Is there a way to do this? 有没有办法做到这一点?

for file1 in os.listdir(path):
    file1name = os.fsdecode(file1)
    if file1.endswith('.m'):
        file2 = file1name.replace('.m', '.txt')
        file2name = os.fsdecode(file2)

        matrix = np.matrix(scipy.io.mmread(os.path.join(path, file1name)).toarray())
        vector = np.loadtxt(os.path.join(path, file2name)
        continue

This is how I would approach something like this given- 这就是我将如何处理这种给定的方式-

I need to do and store some result for each pair of files Each pair is independent The reason the code above isn't enough is because you wanted to try and speed things up. 我需要为每对文件存储一些结果每对文件都是独立的上面的代码不够用的原因是因为您想尝试加快处理速度。

Don't know if this will help, but it is one approach. 不知道这是否有帮助,但这是一种方法。

pool = ThreadPool()
op_names = {name.split('.')[0] for name in os.listdir(path)}
def operation(name):
  matrix_filename = f'name.m'
  vector_filename = f'name.txt'
  return name, do_op(matrix_filename, vector_filename)

results = pool.map(operation, op_names)

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

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