Python Lists

Started by
5 comments, last by Oluseyi 17 years, 6 months ago
I'm trying to (re)learn python after using almost soley c++ for a long time. It's pretty hard because its so much more simple its taking some getting used to. anyway, my problem is i cant figure out how to make an array, or list in python, of a certain size. I know i can go like "var = ['a','b','c'], but I dont want to supply each element like that... i'm looking for python's equivelant of c++'s 'new' keyword
Advertisement
Python's lists dynamically grow. There's rarely a reason to create an "empty" list of size N.

Nevertheless:
>>> l = range(10)>>> l[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> l = [None for i in range(10)]>>> l[None, None, None, None, None, None, None, None, None, None]


Helpful?
well, the reason for me doing this is a good ol' tile-based 2d game. I needed an array for the tiles and i didnt understand how to do that. so i was looking for a python version of tiles = new tile[20][20], but i'm guessing if they dynamically grow there would be a simpler way to do that?
List comprehensions make this really easy

listSize = 10
defaultValue = None
lst = [ defaultValue for x in range(listSize) ]

However, you should be asking yourself - "Why do I need to do this?" Most of the time you're using python you're operating on a data set provided by something else (eg. from file) OR you can just add elements to the list as you go with the append function.

Typically you only need to do things like this for image/signal processing type applications, and in those cases other libraries may provide better support.

[edit]To slow ;-)[/edit]
class Tile:	passtiles = [[ Tile() for x in range(20) ] for y in range(20) ]
Quote:Original post by glBender
well, the reason for me doing this is a good ol' tile-based 2d game. I needed an array for the tiles and i didnt understand how to do that. so i was looking for a python version of tiles = new tile[20][20], but i'm guessing if they dynamically grow there would be a simpler way to do that?

You can use a nested list comprehension:
tiles = [[None for j in range(20)] for i in range(20)]


You can append new elements as you create them:
tiles = []for i in range(20):    c = []    for j in range(20):        c.append(<element>)    tiles.append(c)


Or you can even create a class to represent your tile map, which can use all manner of internal representations, from a dictionary indexed by tuples to a list of lists in sparse matrix configuration. Your more fundamental challenge is deciding on the salient points of your design, not expressing it. [smile]
Ok, why am i doing this... I want a 2d array. this is tough, going from c++ to python makes me feel like a toddler
Quote:Original post by glBender
Ok, why am i doing this... I want a 2d array. this is tough, going from c++ to python makes me feel like a toddler

That's why we encourage people to learn multiple languages - preferrably languages that express different philosophies. It forces you to think about different ways of solving the same problems, with different tradeoffs.

In Python, memory management is not a concern, and even complex structures are most commonly thrown together as compositions of the basic Python types of list, dictionary and tuple. Rather than creating a complex representation, because of the genericity of access and duck typing, focus more on how you would solve problems using the data structure and use a simple placeholder of lists or something in the interim. Later, when that becomes inefficient, you'll probably have sufficient perspective to design a better storage approach.

For example, in writing early sprite games, I just use pygame.Rect for my game objects, then inject member variables into the instance, then subclass Rect, then finally design a proper class derived from pygame.Sprite.

Don't worry about storage. Worry about using your tilemap for now.

This topic is closed to new replies.

Advertisement