Skip to main content
gamedev.net
🔒 Locked

Dynamically allocating memory in python

Started by Iron Eye Jul 1, 2004 at 6:58 AM 3 replies 1k views
Original Post
Iron Eye
Iron Eye
I've recently begun to dabble in python. I have come across a problem where I can think of no other solution than to have a list which is custom tailored to the size I need it to be at runtime. The code I have written this far opens a text file and reads through it line by line. Grabbing paths to images and flag codes as it goes. I want to store all the data in a list of classes so I can access each class by its index number. My map will be nothing more than an array of numbers (which refer to the index of the images in this "list of classes"). I thought about using a stack but I would have to traverse the the entire stack to find the right class. This operation needs to be done often. Is there a way I can allocate more space in my lists on the fly? Or is my mapping method just the problem? Perhaps I'm not seeing a much better way to store map data. -Kris
Gorg
Gorg
You need a dictionary.

dictionary = {}...loop whatever      dict[index] = value...latervalue = dict[index]


more info on dictionaries.

http://docs.python.org/tut/node7.html#SECTION007400000000000000000
http://docs.python.org/lib/typesmapping.html

Fruny
Fruny
Gorg, I suggest value = dictionary.get(index, placeholder) so that you get a placeholder image if you happen to request an invalid index.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Iron Eye
Iron Eye
Well I don't think I really needed a dictionary. With one of the links Gorg provided me I discovered that lists have an append() method (as well as extend() which is vaguely similar), which is exactally what I needed. Dictionaries contain immutable keys (classes are mutable).


Thanks for the indirect help,
Kris
C-Junkie
C-Junkie
You wouldn't use the class as the key, otherwise to get the calss, you'd need the class... ummmm.

You'd use the index as the key.

But, you're using lists anyway...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.