Python 如何分配与输入参数相对应的未知数量的变量

Python 如何分配与输入参数相对应的未知数量的变量,python,matrix,command-line-arguments,allocation,Python,Matrix,Command Line Arguments,Allocation,我有n个files.txt,其中每个文件都包含一个矩阵。我想为每个矩阵分配一个变量,读取相应的文件 我事先不知道命令行参数指定的文件数 因此,我需要一种“for循环”,它一次读取一个文件,并将相应的矩阵分配给一个变量 例如: python allocate_matrices.py file1 file2 file3 其中: 文件1是: 0 1 2 3 4 5 6 7 8 文件2是: 0 1 3 4 6 7 文件3是: 0 1 2 5 3 4 5 6 6 7 8 7 我的想法是使用一种循环

我有n个files.txt,其中每个文件都包含一个矩阵。我想为每个矩阵分配一个变量,读取相应的文件

我事先不知道命令行参数指定的文件数

因此,我需要一种“for循环”,它一次读取一个文件,并将相应的矩阵分配给一个变量

例如:

python allocate_matrices.py file1 file2 file3
其中: 文件1是:

0 1 2
3 4 5
6 7 8
文件2是:

0 1
3 4
6 7
文件3是:

0 1 2 5
3 4 5 6
6 7 8 7
我的想法是使用一种循环:

import numpy as np

for i in range(len(sys.argv)-1):
    file[i] = np.loadtxt(sys.argv[i+1])
但是我不知道我应该用什么样的结构

我事先不知道命令行参数指定的文件数

当然可以:
len(sys.argv)-1

即使没有,Python中的列表也不是固定大小的。那么,为什么不这样做:

matrices = []                       # Allocate an empty list
for filename in sys.argv[1:]:       # Skip argv[0], name of script
    mat = np.loadtxt(filename)      # Read a matrix
    matrices.append(mat)            # Append it to the list

非常感谢。我可以用矩阵[0]调用第一个矩阵,用矩阵[0][0]调用第一个矩阵的第一行,用矩阵[0][I][j]调用元素(I,j),对吗?