python array howto's

1 view
Skip to first unread message

donkyhotay

unread,
Mar 15, 2010, 5:36:54 PM3/15/10
to project...@googlegroups.com
This is mostly for zaak but if someone else knows the answer they are
more then welcome to jump in. Currently I'm working on adding hotseat
capabilities into MoonPy. Because of the way the client/server system
works just trying login again breaks things (horribly) on the client
end. My plan is to modify the client/server so that instead of the
client both recognize multiple players for one client. This means that
many of the variables the server keeps track of like energy, playerID,
teamdID, etc. will need to be turned into lists to keep track of
everything. That by itself I know how to do, however the information
already stored in lists is a little trickier. With C++ I'd just create a
2D array and call it done. From my research the python way of doing this
is to create a "list of lists". The basic creation of this I can find
all over the web.

mylist = [[varx1, varx2, varx3], [vary1, vary2, vary3]]

The issue comes in that I need to be able to adjust these at will. One
of the lists of course (I'm planning the first one) will represent the
playerID of each player connected via the specific client. The second
part will represent the data that needs to be kept track of for each
player. A good example is a list of all the units that a player has
disabled (since undisabling happens after the turn of the person who
disabled a unit). Player 1 on a client may have no units disabled, while
player 2 gets a good hit with an EMP and managed to disable 5 units.
Next turn of course this will change. Right now with a regular list I
just append and pop/remove variables in the list as necessary. How do I
do this for the individual lists inside the 2d list rather then adding
on say a 3rd list (which I don't need)?


--
Do not be afraid to joust a giant just because some people believe in
windmills.
FOSS Moonbase Commander available @ http://code.google.com/p/tether

W Isaac Carroll

unread,
Mar 15, 2010, 5:54:25 PM3/15/10
to project...@googlegroups.com

It would be nice if you could have a friendlier data structure for
passing the information, but I don't know if the network code you're
using allows that.

If you are stuck with lists of lists then fortunately it's pretty
simple (though somewhat ugly).

The basic idea is that once you can talk about one of the nested lists
you can do anything with it that you could do with a list by itself.
For example:

mylist = [[varx1, varx2, varx3], [vary1, vary2, vary3]]

mylist[0].append(fred)

mylist now has [[varx1, varx2, varx3, fred], [vary1, vary2, vary3]]

If you're going to be doing a lot with a particular nested list you
can give it a handy name:

inside_job = mylist[1]

Then you can use it without having to say [1] all the time.

TTFN

donkyhotay

unread,
Mar 15, 2010, 6:42:53 PM3/15/10
to project...@googlegroups.com
Thats what I needed to know, it's an ugly hack job and it would probably
be better to use classes or something similar however I haven't yet
figured out a way to do that without breaking everything.


Do not be afraid to joust a giant just because some people believe in
windmills.
FOSS Moonbase Commander available @ http://code.google.com/p/tether

Reply all
Reply to author
Forward
0 new messages