|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
Quote:
Any particular reason you aren't using Python 2.3? Quote:
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. |
|
#3
|
|||
|
|||
|
you could use the range function slightly differently:
Code:
for x in range(1, len(your_list)):
...
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Troubles splicing lists |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|