Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Aug 28th, 2006, 8:43 AM   #1
flebber
Newbie
 
Join Date: Apr 2006
Location: Newcastle, Australia
Posts: 4
Rep Power: 0 flebber is on a distinguished road
Clarification Nested Lists

Hello, I was reading Python Essential Reference by Beazley. It had the following example on nested lists
Quote:
a = [1,”Dave”,3.14, [“Mark”, 7, 9, [100,101]], 10]
a[1] # Retruns Dave
a[3][2] # Returns 9
a[3][3][1] # Returns 101


I just need to confirm something I am having trouble clarifying with the other docs I have found - I think maybe its too simple a question.

Could I reference 7 above with a[4] as well as a[3][1]? So again referring to the above example of a[3][3][1] returns 101 why couldn't it be referenced as a[6][1] or do I need a new [] to represent each nest in the list?

Finally could I negative reference a[-1] to equal 101 or does this create too much confusion ?
__________________
The problem with reality is that leaves a lot to the imagination - John Lennon
flebber is offline   Reply With Quote
Old Aug 28th, 2006, 8:59 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by flebber
Could I reference 7 above with a[4] as well as a[3][1]? So again referring to the above example of a[3][3][1] returns 101 why couldn't it be referenced as a[6][1] or do I need a new [] to represent each nest in the list?
You seem to be in a bit of a muddle concerning how nested lists work. Let me try and illustrate the above example using a different approach that should make this more clear:
python Syntax (Toggle Plain Text)
  1. c = [100, 101]
  2. b = ["Mark", 7, 9, c]
  3. a = [1, "Dave", 3.14, b, 10]
  4.  
  5. a[1] # Returns Dave
  6. a[3] # Returns the list referenced by b
  7. a[3][2] # Returns b[2], which is 9
  8. a[3][3][1] # Returns b[3][1], which is the same as c[1], which is 101
Or, to put use an analogy, think of it as a set of boxes with compartments. Box c has two compartments, with 100 and 101 inside it. Box c sites inside the fourth compartment of box b, and box b sits in the fourth compartment of box a.

To get to box b, one must first open the relevant compartment of box a (in programming terms, that's what a[3] does). To get to box c, one must first open the relevant compartment of box b (which is b[3], or a[3][3], since a[3] is just another way of getting b).

Does that make it clearer?

Quote:
Originally Posted by flebber
Finally could I negative reference a[-1] to equal 101 or does this create too much confusion ?
a[-1] references the last element of a, which is 10.
Arevos is offline   Reply With Quote
Old Aug 29th, 2006, 6:09 AM   #3
flebber
Newbie
 
Join Date: Apr 2006
Location: Newcastle, Australia
Posts: 4
Rep Power: 0 flebber is on a distinguished road
Thanks Arevos, thanks for the reply and clearing that up for me. I thought thats what it was but you know sometimes the simple things trip you up and can be the hardest things to find answers too.

Thanks Again

fleb
__________________
The problem with reality is that leaves a lot to the imagination - John Lennon
flebber is offline   Reply With Quote
Old Aug 31st, 2006, 10:24 AM   #4
magnus.therning
Programmer
 
Join Date: May 2006
Location: Cambridge, UK
Posts: 36
Rep Power: 0 magnus.therning is on a distinguished road
Just a small tip, python's interpreter has an interactive mode which you can use to get some hands-on experience with the basics of the language. Just run "python" on the command line.

I can recommend IPython as an "interactive python on steroids". :-)
__________________
Don't comment bad code - rewrite it.
- The Elements of Programming Style (Kernighan & Plaugher)
magnus.therning 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
Assigning an array of lists deanosrs C 42 Apr 13th, 2006 1:35 PM
Linked Lists Eric the Red C++ 6 Mar 19th, 2006 12:47 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
Nested Loop Question kent666 C++ 3 Feb 26th, 2005 10:21 AM




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

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