Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 11th, 2004, 05:14 PM
Theeggman Theeggman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 266 Theeggman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 33 sec
Reputation Power: 8
Troubles splicing lists

I am trying to skip the first element of the list build_pass_list[j] when using a for loop. I have been unable to figure out how to splice of the [1:]. I am assuming for now that because it is a sublist it does not have the same properties as the list itself. Python 2.2.2

Splicing the back end of the lists works.
Code:
build_pass_list[j][:1]


Splicing the front end does not work.
Code:
build_pass_list[j][1:])


Here is the entire for loop
Code:
for j in range(len(build_pass_list)):
for k in range(len(build_pass_list[j][1:])):
[INDENT]print k

Reply With Quote
  #2  
Old June 11th, 2004, 07:50 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Quote:
Originally Posted by Theeggman
I am trying to skip the first element of the list build_pass_list[j] when using a for loop. I have been unable to figure out how to splice of the [1:]. I am assuming for now that because it is a sublist it does not have the same properties as the list itself. Python 2.2.2

Any particular reason you aren't using Python 2.3?

Quote:
Splicing the back end of the lists works.
Code:
build_pass_list[j][:1]


Splicing the front end does not work.
Code:
build_pass_list[j][1:])


Could it be because of the stray ')' you have in there? Because slicing is the same from either end. Without specifics on what errors you are getting or what incorrect behavior you are seeing, I'm not sure what's going wrong for you.
__________________
Debian - because life's too short for worrying.
Best. (Python.) IRC bot. ever.

Reply With Quote
  #3  
Old June 12th, 2004, 01:13 AM
rebbit rebbit is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 84 rebbit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 7 m
Reputation Power: 5
you could use the range function slightly differently:

Code:
for x in range(1, len(your_list)):
    ...

Reply With Quote
  #4  
Old June 12th, 2004, 04:26 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,243 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 5 h 41 m 59 sec
Reputation Power: 265
When you say it doesn't work, what does that mean? Does it throw an exception, and if so then what is it? Or does it not print the results you expect? If so then what do you expect, and what does it actually print?

What is in build_pass_list? If it contains any non-indexible objects then it will throw an exception.

In the outer loop why do you use range to get an index into the list, instead of stepping through the list elements directly?

e.g. instead of

Code:
for j in range(len(build_pass_list)):
    for k in range(len(build_pass_list[j][1:])):
        print k


do


Code:
for j in build_pass_list:
    for k in range(len(j[1:])):
        print k


In the inner loop, what do you expect range(len(j[1:])) to produce? It creates a list of the numbers 0 to N-1, where N is the length of the list j. Are you expecting it to produce the numbers 1 to N?

Dave - The Developers' Coach

Reply With Quote
  #5  
Old June 12th, 2004, 05:09 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,243 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 5 h 41 m 59 sec
Reputation Power: 265
BTW, a small point about terminology.

What you are talking about is slicing lists - extracting a sub-list using the [:] notation.

Splicing is when a second list is inserted into a list by assigning it to a slice.

Examples of splicing:
Code:
>>> x = range(10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x[1:1] = ['foo']  #insert a list 
>>> x
[0, 'foo', 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x[4:7] = ['bar']  #replace a sublist
>>> x
[0, 'foo', 1, 2, 'bar', 6, 7, 8, 9]
>>> 


Dave - The Developers' Coach

Reply With Quote
  #6  
Old June 12th, 2004, 02:01 PM
Theeggman Theeggman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Posts: 266 Theeggman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 33 sec
Reputation Power: 8
Group answer:

Rebbit: Worked like a champ. Exactly what i needed.

Strike: I'm using 2.2 because I don't have a choice in the matter. Secondly it was not throwing an error it was just not starting the loop from the second element. From playing around on the python command line the syntax I was using appeared to be correct.

Devcoach: Noted.

Thanks for the responses. Problem solved.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Troubles splicing lists


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT