我在文本文件中具有要分配给数组C [5] [100]的整数。我的数据是这种格式:
17 40 35 24 50 15 31 38 48 18 16 44
41 10 26 50 48 20 24 12 48 24 34 39
...............
我正在尝试下面的代码,但是我得到的错误是:
[ValueError:无法将大小为1005的序列复制到维度为100的数组轴上
text_file = open("c051001.txt", "r")
C=np.zeros((5,100))
for i in range(agent):
C[i,]=map(int, (value for value in text_file.read().split()))
文件中的整数数量大于500,但是我想将其余的数字分配给另一个数组。
您需要将数据分成适当的块。一个简单的方法可以是:
agent = 5
resource = 1
sz = 100
C = np.zeros((agent, sz))
idx = 0
chunk = sz
for i in range(agent):
C[i, ] = list(map(int, data[idx:idx + chunk]))
idx += chunk
# Assign the following 500 integers into another array of A[5,100,1]
A = np.zeros((agent, sz, resource))
for k in range(resource):
for i in range(agent):
A[i, :, k] = list(map(int, data[idx:idx + chunk]))
idx += chunk
trailing_data = data[idx:]