Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
meta array question
#1
I'm modelling a 3D space.

I'd like to create a large multi dim array , like numpy.empty((720,720,720),dtype=object) in size, but anticipate for most of the program run time, few of the array elements will be used. The array indices are more for reference.

Is just using numpy.empty a decent way to do this? I'm not sure that initially creating 720x720x720 references to None objects is efficient.

Is a way a better way to dynamically create multii dim array?
Reply
#2
Do you really need to save the memory? That's a pretty big chunk, but if running this is all the machine will be doing anyway it probably wouldn't make much of a difference, especially if you have to write more code.

If you really want to save memory, are you considering not allocating the cubic space at all, and only using values you write to? Are you asking how to do that?
Reply
#3
>Are you asking how to do that?

Yes. The idea is to list objects in each cubical.
But at the start the space will be sparely populated. I thought maybe a dictionary would be better i.e.

cubic = {}
cubic['231 098 045' ] =[ship,planet,moon,rock,star]
No need to set all the other cubicals as None
I'd need a bit more code to convert the dictionary entry names into integers triples though.
Reply
#4
At first I was thinking of a nested dictionary structure, but then I realized that you can actually just use tuples as dictionary keys.

Do you need to do anything with ranges? Or are you always just checking specific "cubicles"? Tuple keys would be great in the latter case but in the former case things get potentially more complicated.
Reply
#5
awesome. In my training, it never came up that dictionary keys could be integers or tuples. We only used strings as dictionary keys.
Reply
#6
Wow! Yeah, the keys can be anything immutable, meaning you can't change its value. For example, you can't change a string, tuple or integer in-place, you have to replace the whole variable. Contrast that with a list, which can change. The dictionary would then have issues if the way it organizes things internally relies on the keys not changing, so that's a high-level explanation of why.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  np.array question hissonrr 3 1,967 Apr-20-2020, 01:15 PM
Last Post: hissonrr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020