Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Are nested lists indestructable? (http://www.programmingforums.org/showthread.php?t=15762)

Chuckiferd May 3rd, 2008 3:29 PM

Are nested lists indestructable?
 
Hello I am trying to create a program that involves writing a whole bunch of lists with information of its specific topic then grouping them together. Normally I would use a hash but they don't really let you create a whole bunch of variables that can be withdrawn later.

So my problem is when I nest them together, based on a random factor I want to delete so many of them from the back, but nothing I write seems to work

I can see doing all this with a whole bunch of if: loops but I know there has to be some easier way

here is my program(at least the part that pertains to it)
(PS. all the importing and preperations are done I simply didn't print it)

a=randint(6,11)
aa=['name', 'att', 'def', 1]
ab=['name', 'att', 'def', 1]
ac=['name', 'att', 'def', 1]
ad=['name', 'att', 'def', 1]
ae=['name', 'att', 'def', 1]
af=['name', 'att', 'def', 1]
ag=['name', 'att', 'def', 1]
ah=['name', 'att', 'def', 1]
ai=['name', 'att', 'def', 1]
aj=['name', 'att', 'def', 1]
abspirlist=[aa,ab,ac,ad,ae,af,ag,ah,ai,aj]
del abspirlist[a:]
shuffle(abspirlist)

Sane May 3rd, 2008 3:45 PM

Re: Are nested lists indestructable?
 
Are you trying to turn:

abspirlist=[aa,ab,ac,ad,ae,af,ag,ah,ai,aj]

Into a list terminated at a random value, like:

abspirlist=[aa,ab,ac,ad,ae,af]

Why not do:

:

abspirlist=[aa,ab,ac,ad,ae,af,ag,ah,ai,aj]
abspirlist = abspirlist[:a]


Where a is your random integer. Or do you want to keep the first dimension in tact, and pop elements from each array in the second dimension? In that case, just use a for loop, with the same code as above used on each nested list individually.

Chuckiferd May 3rd, 2008 5:27 PM

Re: Are nested lists indestructable?
 
whoa, why is it suddenly working, I must of been doing something wrong. Weird I could of swore that, that was the first thing I tried.

Thanks Sane

PS. Is there a way to automaticaly count the number of contents, (or nestled lists) in a list, if not I am going to have to make a mod because it would make everything so much easier

Sane May 3rd, 2008 5:43 PM

Re: Are nested lists indestructable?
 
So if you had:

:

abspirlist = [['name', 'att', 'def', 1],
              ['name', 'att', 'def', 1],
              ['name', 'att', 'def', 1]]


You want the number 12, for the total number of nested elements?

You can do that with a little fancy work, by generating a list of lengths (in this case: [4, 4, 4]).

Then feeding it to the sum function (sum([4,4,4]) evaluates 12).

Combining these two ideas gives us:

:

sum((len(x) for x in abspirlist))

Chuckiferd May 3rd, 2008 6:24 PM

Re: Are nested lists indestructable?
 
cool then I can divide that by 4 to figure out how many nested lists there are.

You know ever since last night everything seems to be going wrong, I am adding the code to make each list unique and different. Look at whats happening

:

namelist=['Fred', 'Joe', 'Edward', 'Eric', 'John', 'Ryan', 'Chuck', 'Henry', 'Jean', 'Robert', 'Guido', 'Alan', 'Nicolas', 'Bill']
def whrstat():
  dice=randint(0, 13)
  whrname=namelist[dice]
  att=randint(1, 5)
  deff=randint(1, 5)

whrstat()
att                              x
deff                            y
aa=[whrname, att, deff, 1]
whrstat()                     
att                                x
deff                              y; ect. ect. ect.
ab=[whrname, att, deff, 1]
whrstat()
att
def
ac=[whrname, att, deff, 1]
whrstat()
att
def
ad=[whrname, att, deff, 1]
whrstat()
att
def
ae=[whrname, att, deff, 1]
whrstat()
att
def
af=[whrname, att, deff, 1]
whrstat()
att
def
ag=[whrname, att, deff, 1]
whrstat()
att
def
ah=[whrname, att, deff, 1]
whrstat()
att
def
ai=[whrname, att, deff, 1]
whrstat()
att
def
aj=[whrname, att, deff, 1]
abspirlist=[aa,ab,ac,ad,ae,af,ag,ah,ai,aj]
dice=randint(0,4)
dice
del abspirlist[:dice]
shuffle(abspirlist)


Lets just say x=4 and y=3

:

>>>print abspirlist
[Guido, 4, 3, 1]

Sane May 3rd, 2008 6:33 PM

Re: Are nested lists indestructable?
 
Wait, what? Hold on a tick. Divide by 4?

If you just want the number of lists, then use the len function...

:

len(abspirlist)

That will give you 3 in my example.

And in the code you posted, don't do:

:

del abspirlist[:dice]

Reassign your array in the same manner I showed you in my second post.

:

x = x[a:]  # Will keep all elements from the a'th element to the end
x = x[:a]  # Will keep all elements until the a'th element


Chuckiferd May 3rd, 2008 7:46 PM

Re: Are nested lists indestructable?
 
got it that was actually old un-updated code, I fixed all that, but do you get why the number generator is not working

Sane May 3rd, 2008 10:37 PM

Re: Are nested lists indestructable?
 
It's not that it's not working. What appears to be a lack of randomness is actually a side-effect of not creating new variables.

When you create variables inside a function, they need to be returned back to where the function was called if you want to access them.

:

from random import randint

namelist=['Fred', 'Joe', 'Edward', 'Eric', 'John', 'Ryan', 'Chuck', 'Henry', 'Jean', 'Robert', 'Guido', 'Alan', 'Nicolas', 'Bill']

def whrstat():
  dice=randint(0, 13)
  whrname=namelist[dice]
  att=randint(1, 5)
  deff=randint(1, 5)
  return [whrname, att, deff, 1]

aa = whrstat()
print aa

ab = whrstat()
print ab


:

['John', 4, 2, 1]
['Henry', 1, 2, 1]


What that will do is create the variables inside the function, return them as a list, and then assign them to whatever's calling the function.

If the explanation for this behaviour does not seem obvious to you, I suggest that you do a little review on how to use functions and return.

Chuckiferd May 3rd, 2008 11:40 PM

Re: Are nested lists indestructable?
 
ok I see, I always get so caught up in one method that I never think of changing it.


All times are GMT -5. The time now is 4:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC