The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Troubles splicing lists
Discuss Troubles splicing lists in the Python Programming forum on Dev Shed. Troubles splicing lists Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 11th, 2004, 04:14 PM
|
|
Contributing User
|
|
Join Date: May 2001
Posts: 266
Time spent in forums: 30 m 33 sec
Reputation Power: 13
|
|
|
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
|

June 11th, 2004, 06:50 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
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.
|

June 12th, 2004, 12:13 AM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 84
Time spent in forums: 8 h 7 m
Reputation Power: 10
|
|
you could use the range function slightly differently:
Code:
for x in range(1, len(your_list)):
...
|

June 12th, 2004, 03:26 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
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
|

June 12th, 2004, 04:09 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
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
|

June 12th, 2004, 01:01 PM
|
|
Contributing User
|
|
Join Date: May 2001
Posts: 266
Time spent in forums: 30 m 33 sec
Reputation Power: 13
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|