Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 3rd, 2008, 2:29 PM   #1
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
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)
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Old May 3rd, 2008, 2:45 PM   #2
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
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.
Sane is offline   Reply With Quote
Old May 3rd, 2008, 4:27 PM   #3
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
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
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Old May 3rd, 2008, 4:43 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
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))
Sane is offline   Reply With Quote
Old May 3rd, 2008, 5:24 PM   #5
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
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]
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Old May 3rd, 2008, 5:33 PM   #6
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
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
Sane is offline   Reply With Quote
Old May 3rd, 2008, 6:46 PM   #7
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
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
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Old May 3rd, 2008, 9:37 PM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,722
Rep Power: 5 Sane is on a distinguished road
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.
Sane is offline   Reply With Quote
Old May 3rd, 2008, 10:40 PM   #9
Chuckiferd
Programmer
 
Join Date: Nov 2007
Posts: 61
Rep Power: 1 Chuckiferd is on a distinguished road
Re: Are nested lists indestructable?

ok I see, I always get so caught up in one method that I never think of changing it.
__________________
There are 10 kinds of people in this world, those who can read binary and those who can't.
Chuckiferd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointers and Linked Lists BstrucT C 8 Feb 11th, 2008 2:33 AM
Sorting integer lists sixstringartist Python 4 Dec 4th, 2007 2:27 PM
Clarification Nested Lists flebber Python 3 Aug 31st, 2006 10:24 AM
Nested Lists Mjordan2nd Python 2 Oct 22nd, 2005 3:41 PM
For Each ... Next, Nested Array issues. ChefBoiAreDee Visual Basic 2 Mar 2nd, 2005 4:26 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:38 AM.

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