1

I have integer numbers in a text file to be assigned to an array C[5][100]. My data is in this format:

17 40 35 24 50 15 31 38 48 18 16 44 
41 10 26 50 48 20 24 12 48 24 34 39 
...............

I am trying the code below but the error I get is this:

ValueError: cannot copy sequence with size 1005 to array axis with dimension 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()))

Number of integers in the file is more than 500 but I want to assign the remainder of numbers to another array.

larry79
  • 13
  • 3

1 Answers1

0

You need to divide the data into appropriate chunks. A simple way to do this could be:

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:]

FredrikHedman
  • 1,223
  • 7
  • 14
  • Works perfect thank you FredrikHedman! I tried to use the same block to assign the following 500 integers into another array of A[5,100,1] but I could not find the right syntax. This is what I thought that would work but it is not: chunk = 100 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:] – larry79 Mar 13 '20 at 05:18
  • I updated the suggested solution. Still, seems like a good idea to have a good look at https://numpy.org/devdocs/user/quickstart.html#indexing-slicing-and-iterating – FredrikHedman Mar 13 '20 at 09:30
  • Also have a look at https://stackoverflow.com/help/someone-answers – FredrikHedman Mar 13 '20 at 09:37