![]() |
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) |
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] 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. |
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 |
Re: Are nested lists indestructable?
So if you had:
:
abspirlist = [['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)) |
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']Lets just say x=4 and y=3 :
>>>print abspirlist |
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 |
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
|
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:
['John', 4, 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. |
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