very simplePython question: How to dynamically create lists?

Okay here is a problem I am having and cannot find the answer after searching for several hours on the various python forums and references:

How do I create lists dynamically?

here is very simple piece of code I am working on:

import random
a=[]
for i in range(0, 120):
    c='card' + str(i)
    a.append(c)
random.shuffle(a)
card_files=open("carddb.db")
data1=card_files.read()
cdata=[]
for d in data1.split("^"):
    cdata.append(d)
    print len(cdata)-1

for j in range(0,  (len(cdata)-1)):
    print j
    txt=cdata[j]
    print txt

in the last part I need to be able to dynamically create lists using the variable j:

I know the following is wrong but it shows what I want to do:

    card + str(j)=[]
    card+str(j).append(txt)

Thank you
JB Skaggs

Actually no that isn’t really clear. What does “card + str(j)” mean?

I think you are referring to:

discourse.panda3d.org/viewtopic.php?t=6228

I just found this other way

vars()["MyListName"]=[]
MyListName.append(text)

EDIT: I just saw that the other two posts were added as I was typing this response. Thanks guys.


what I mean is when my for loop runs that I want to dynamicaly create a brand new list with a name combined from the text “card” and the value of the integer j.

so if j = 1 then my new list would be called “card1”

Then that list called “card1” would hold all the data for that card.

the text file is split into individual 100 cards (several are duplicated) here is an example of a card’s data such as:
fc1#f#c#0/1 fire boy#0#1#fc1.jpg#hive#0#0#0

the data would be split at the “#” characters.

creating a list:
card1= (fc1,f,c,0/1 fire boy,0,1,fc1,jpg,hive control,0,0,0)

then
card2
card3
card4 etc

In other words

card_files=open("carddb.db")
data1=card_files.read()
cdata=[]
for d in data1.split("^"):
    cdata.append(d)
    print len(cdata)-1

for j in range(0,  (len(cdata)-1)):
    print j
    txt=cdata[j]
    print txt
    for e in txt.split("#"):
        card1=[] <<<<<<<<<< how do I create this name dynamically for each of the 100 +\- cards???
        card1.append=[e]

Does that clarify my intent?

These carddata are used in combat in the game we are working on and hold the reference data for textures, hitpoints, spell effects etc. So that when a card is dealt it calls all the associated data for that card and initializes sfx, fx, animations etc.

I have pasted a dump of this datafile at the base of this email if it would help.

JB SKaggs

the data file I am trying to import into lists:

fc1#f#c#0/1 fire boy#0#1#fc1.jpg#hive#0#0#0^fc2#f#c#0/1 spark#0#1#fc2.jpg#hive#0#0#0^fc3#f#c#1/1 imp#1#1#fc3.jpg#n#0#0#0^fc4#f#c#1/1 fire dog#1#1#fc4.jpg#n#0#0#0^fc5#f#c#2/2 djinni#2#2#fc5.jpg#n#0#0#0^fc6#f#c#2/4 fire serpent#2#4#fc6.jpg#n#0#0#0^fc7#f#c#3/3 nigtmare#3#3#fc7.jpg#n#0#0#0^fc8#f#c#3/3 fire dragon#3#3#fc9.jpg#n#0#0#0^fc9#f#c#4/4 fire giant#4#4#fc9.jpg#n#0#0#0^fc10#f#c#5/5 firebunny horde#5#5#fc10.jpg#n#0#0#0^fc11#f#c#6/6 volkan#6#6#fc11.jpg#n#0#0#0^fw1#f#w#0/3 wall of fire#0#3#fw1.jpg#n#0#0#0^fw2#f#w#3/5 fire castle#3#5#fw2.jpg#n#0#0#0^fs1#f#s#+2 flame of life#0#0#fs1.jpg#2ht#0#0#0^fs2#f#s#-3 fireball#0#0#fs2.jpg#-3t#0#0#0^fs3#f#s#burning hail#0#0#fs3.jpg#-2a#0#0#0^fs4#f#s#lose turn - welded flesh#0#0#fs4.jpg#skip#0#0#0^fs5#f#s#haste (dbl attack) - fiery speed#0#0#fs5.jpg#2atk#0#0#0^fs6#f#s#counter spell - Flare#0#0#fs6.jpg#nix#0#0#0^fs7#f#s#blind- can't block - Blinding Smoke#0#0#fs7.jpg#blnd#0#0#0^fs8#f#s#reflect - attacker takes effect- Shield of flames#0#0#fs8.jpg#rflk#0#0#0^fs9#f#s#remove creature- incinerate#0#0#fs9.jpg#rmv#0#0#0^fs10#f#c#resurrect- play creature from discard pile#0#0#fs10.jpg#rsrc#0#0#0^wc1#w#c#0/1 seamonkey#0#1#wc1.jpg#hive#0#0#0^wc2#w#c#0/1 squirt#0#1#wc2.jpg#hive#0#0#0^wc3#w#c#1/1 sprite#1#1#wc3.jpg#n#0#0#0^wc4#w#c#1/1 illithimon#1#1#wc4.jpg#n#0#0#0^wc5#w#c#2/2 dire polar bear#2#2#wc5.jpg#n#0#0#0^wc6#w#c#2/4 water spout#2#4#wc6.jpg#n#0#0#0^wc7#w#c#3/3 kraken#3#3#wc7.jpg#n#0#0#0^wc8#w#c#3/3 Sea Serpent#3#3#wc8.jpg#n#0#0#0^wc9#w#c#4/4 water giant#4#4#wc9.jpg#n#0#0#0^wc10#w#c#5/5 drowned pirates#5#5#wc10.jpg#n#0#0#0^wc11#w#c#6/6 neptune#6#6#wc11.jpg#n#0#0#0^ww1#w#w#0/3 wall of water#0#3#ww1.jpg#n#0#0#0^ww2#w#w#3/5 floating city#3#5#ww2.jpg#n#0#0#0^ws1#w#s#+2 health - healing mist#0#0#ws1.jpg#+2ht#0#0#0^ws2#w#s#-3 water strike#0#0#ws2.jpg#-3t#0#0#0^ws3#w#s#-2 all opp - Typhoon#0#0#ws3.jpg#-2a#0#0#0^ws4#w#s#lose turn - Ice prison#0#0#ws4.jpg#skip#0#0#0^ws5#w#s#haste - (dbl attack) White Waters#0#0#ws5.jpg#2atk#0#0#0^ws6#w#s#counter spell - sneaker wave#0#0#ws6.jpg#nix#0#0#0^ws7#w#s#blind-can't block - Arcane Fog#0#0#ws7.jpg#blnd#0#0#0^ws8#w#s#reflect - attkr takes hit - Ice Mirror#0#0#ws8.jpg#rflk#0#0#0^ws9#w#s#remove creature - drown#0#0#ws9.jpg#rmv#0#0#0^ws10#w#s#resurrect - Water Born#0#0#ws10.jpg#rsrc#0#0#0^kc1#k#c#0/1 willow the wisp#0#1#kc1.jpg#hive#0#0#0^kc2#k#c#0/1 dustdevil#0#1#kc2.jpg#hive#0#0#0^kc3#k#c#1/1 eagle#1#1#kc3.jpg#n#0#0#0^kc4#k#c#1/1 faeries#1#1#kc4.jpg#n#0#0#0^kc5#k#c#2/2 wind riders#2#2#kc5.jpg#n#0#0#0^kc6#k#c#2/4 Storm Queen#2#4#kc6.jpg#n#0#0#0^kc7#k#c#3/3 thunderbirds#3#3#kc7.jpg#n#0#0#0^kc8#k#c#3/3 cloud dragon#3#3#kc8.jpg#n#0#0#0^kc9#k#c#4/4 storm giant#4#4#kc9.jpg#n#0#0#0^kc10#k#c#5/5 raksha raiders#5#5#kc10.jpg#n#0#0#0^kc11#k#c#6/6 saturn#6#6#kc11.jpg#n#0#0#0^kw1#k#w#0/3 wall of wind#0#3#kw1.jpg#n#0#0#0^kw2#k#w#3/5 Sky Fortress#3#5#kw2.jpg#n#0#0#0^ks1#k#s#+2 health - wind of life#0#0#ks1.jpg#+2ht#0#0#0^ks2#k#s#-3 Kamikaze#0#0#ks2.jpg#-3t#0#0#0^ks3#k#s#-2 all opp - insect plague#0#0#ks3.jpg#-2a#0#0#0^ks4#k#s#lose turn - miasma#0#0#ks4.jpg#skip#0#0#0^ks5#k#s#haste (dbl attack) Herme's Winged Sandals#0#0#ks5.jpg#2atk#0#0#0^ks6#k#s#counter spell - breeze of amnesia#0#0#ks6.jpg#nix#0#0#0^ks7#k#s#blind - can't block - stinging vapors#0#0#ks7.jpg#blnd#0#0#0^ks8#k#s#reflect - attacker takes effect- redirection of the air#0#0#ks8.jpg#rflk#0#0#0^ks9#k#s#remove creature - killing wind#0#0#ks9.jpg#rmv#0#0#0^ks10#k#s#resurrect -  Breath of Heaven#0#0#ks10.jpg#rsrc#0#0#0^ec1#e#c#0/1 mud boy#0#1#ec1.jpg#hive#0#0#0^ec2#e#c#0/1 angry bush#0#1#ec2.jpg#hive#0#0#0^ec3#e#c#1/1 goblin#1#1#ec3.jpg#n#0#0#0^ec4#e#c#1/1 skeleton#1#1#ec4.jpg#n#0#0#0^ec5#e#c#2/2 golem#2#2#ec5.jpg#n#0#0#0^ec6#e#c#2/4 tree man#2#4#ec6.jpg#n#0#0#0^ec7#e#c#3/3 elder stone#3#3#ec7.jpg#n#0#0#0^ec8#e#c#3/3 sea serpent#3#3#ec8.jpg#n#0#0#0^ec9#e#c#4/4 mud monster#4#4#ec9.jpg#n#0#0#0^ec10#e#c#5/5 undead army#5#5#ec10.jpg#n#0#0#0^ec11#e#c#6/6 pluto#6#6#ec11.jpg#n#0#0#0^ew1#e#w#0/1 mud wall#0#1#ew1.jpg#n#0#0#0^ew2#e#w#3/5 Mountain Fortress#3#5#ew2.jpg#n#0#0#0^es1#e#s#+2 health- rejuvenating clay#0#0#es1.jpg#+2ht#0#0#0^es2#e#s#-3 tossed boulder#0#0#es2.jpg#-3t#0#0#0^es3#e#s#-2 all opp - landslide#0#0#es3.jpg#-2a#0#0#0^es4#e#s#lose turn - Bogged Down#0#0#es4.jpg#skip#0#0#0^es5#e#s#haste - dbl attack - Earth Walk#0#0#es5.jpg#2atk#0#0#0^es6#e#s#counter spell - sinkhole#0#0#es6.jpg#nix#0#0#0^es7#e#s#blind - can't block - sandstorm#0#0#es7.jpg#blnd#0#0#0^es8#e#s#reflect - crystal wand#0#0#es8.jpg#rlfk#0#0#0^es9#e#s#remove creature- thanatos#0#0#es9.jpg#rmv#0#0#0^es10#e#s#resurrect - Grave Touched#0#0#es10.jpg#rsrc#0#0#0^ai1#a#i#yagriff's cauldron#0#0#ai1.jpg#rsrc#0#0#0^ai2#a#i#ring of health#0#0#ai2.jpg#+2hp#0#0#0^ai3#a#i#poisonous crown#0#0#ai3.jpg#-1p-2o#0#0#0^ai4#a#i#gem of strength#0#0#ai4.jpg#+3dmg#0#0#0^ai5#a#i#mirror of weakness#0#0#ai5.jpg#-1t#0#0#0^ai6#a#i#hive control#0#0#ai6.jpg#+1/+1hive#0#0#0^ac1#a#c#araknalisk#5#5#ac1.jpg#n#0#0#0^ac2#a#c#autonatomic men#1#1#ac2.jpg#hive#0#0#0

ah okay now I get what you want. Personally I would do this with a list of lists; doing it with uniquely named lists feels kinda hackish but I guess works just as well.

I mean something like (pseudocode):
cards = []
cards[1] = []

It can be:

card_files=open("carddb.db")
data1=card_files.read()
cards=[]
for d in data1.split("^"):
    card = d.split("#")
    cards.append(card)

Thanks, I couldn’t think off the top of my head what the actual Python code would look like, haven’t been programming in Python enough yet :stuck_out_tongue:

That does look really elegant. Is it weird that I feel a twinge of jealousy because he gets to use that code in his project while I don’t need it?

When I wrote this first in Gambas the the deck control, shuffling, and dealing the card took almost fifty lines of code! And in python you have helped me to reduce the code down to 15 lines and four of those are to view the output!

I can see I made the rigt decision in moving our teens programming from VB / Gambas (SDL / OPENGL) to Panda3d / Python.

Thanks guys.

here was the fnished test code for the cards deck code:

import random
deck=[]
card_files=open("carddb.db")
data1=card_files.read()
cards=[]
for d in data1.split("^"):
    card = d.split("#")
    cards.append(card) 
print cards
for c in range(0, (len(cards)-1)):
    deck.append(c)
print deck 
random.shuffle(deck)
print deck 
print cards[deck.pop()]

Or if you want it really short and difficult to manage:

cards = [card.split("#") for card in open(“carddb.db”).read().split("^")]

Similarly:
cards = [card.split("#") for card in open(“carddb.db”).read().split("^")]
deck = [i for i in range(len(cards))]
random.shuffle(deck)

:open_mouth:

good lord you were programming in Visual Basic? Dude, come on in from the cold!

For the record jbskaggs, what you wrote 4 posts up is pretty good. I’m pretty sure the kids would flip out if you told them to write this one monster line.

As fun as this is, and trust me, I like it: You open the file descriptor, but don’t close it, which is naughty.

Good for a temporary script, but less good for actual production use.

if you’re using 2.5 you can do

from __future__ import with_statement

with open("carddb.db") as carddb:
     cards = [card.split("#") for card in carddb.read().split("^")]
deck = [i for i in range(len(cards))]
random.shuffle(deck)

in 2.6 you can leave off the from future line.

alternately you can do the more standard.

carddb = open("carddb.db")
cards = [card.split("#") for card in carddb.read().split("^")]
carddb.close()
deck = [i for i in range(len(cards))]
random.shuffle(deck)

as the with will automatically close your file for you.

Good to know.

Now to get my gui’s going.

JB SKaggs