Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

python - Recommended use of Numpy's structured arrays

I want to save 1D arrays into each entry of another 1D array. Essentially I have a list of grid points (1D array) for which each I would like to save a 1D array containing information specific to the grid point. (Imagine for each of a list of cities in a country, saving the demographic age distribution from age 1 to 100.)

While I can think of many ways to theoretically implement this, I am not sure which is the most efficient and in general recommended.

I could save the data in a matrix where each row is a country and the columns specify the age group. That would work and I can implement it.

Alternatively I could save the information as a structured array where the list of countries is an array and each entry is an array with the age distribution. Intuitively I prefer this option, but I don't know whether it's actually recommended. My program runs many thousand iterations, so speed is more important than prettiness of the code. The other problem I have with this is that I need to initialise that array dynamically (I hope I'm using that word correctly). I have a for-loop iterating through the list of points and saving the results for each point. However, since my list of points varies based on the init values, I can't initialise it like so data = np.array([(0, 0, 0, 0, 0), (0, 0, 0, 0, 0), (0, 0, 0, 0, 0), (0, 0, 0, 0, 0)]) because it won't always be a 4x5 array.

Any ideas on what would be the best way to do this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...