0
...
def splitMunipulation(p,threshold=5000):
    runs=[];i=0
    while i<len(p):
        l=[];i+=1
        print i,p[i]
        while p[i]!=press(0,1,0):
            l.append(p[i]);i+=1
        else:       
            runs.append(l)#here i points to another (0,1,0)
    return runs
...

record=splitMunipulation(record)

'''

Output:

    1 <__main__.press instance at 0x046690A8>
      File "H:\mutate.py", line 28, in splitMunipulation
        while p[i]!=press(0,1,0):
    IndexError: list index out of range

pressis a class

and since print p[i] works well,why p[i] is considered out of range?

Really don't get what's going on

'''

1
  • The traceback you've posted doesn't match any line of your code. Commented Apr 6, 2014 at 4:43

3 Answers 3

3

so, a few things..

Firstly, your code is very... unpythonic. This isn't C, so you don't need to use while loops for iteration, and don't use semicolons to separate multiple commands on one line in Python. Ever. Also, the while...else format is confusing and should be avoided.

If you look at the first few 'lines' of your while loop,

while i<len(p):
        l=[];i+=1

You keep i below the length of p, but you immediately increase i's value by one. As such, when i=len(p) - 1, you will make i one larger, len(p). So when you try to access p[i], you are trying to access a value that doesn't exist.

Fixing those issues, you would get:

...
def splitMunipulation(p,threshold=5000):
    runs=[]

    for i in p:
        l=[]
        print i
        if i != press(0,1,0):
            runs.append(i)
    return runs
...

record=splitMunipulation(record)
Sign up to request clarification or add additional context in comments.

2 Comments

I would add don't use the while . . . else construct. Even Guido has said it's a bad idea.
I just realized that this would cause an infinite loop if i was not equal to press(0, 1, 0), so i substituted if for while. Should still give desired output... Looking at it now, I'm not really sure how this code is supposed to work.
2
while p[i]!=press(0,1,0):
   l.append(p[i]);i+=1

The variable i gets incremented in this loop until p[i]!=press(0,1,0). Since nothing is happening to make p longer, or to test that i is not greater than the length of p, it is easy to see how the index could get out of range.

1 Comment

Your print i, p[i] statement is outside the while loop. i is in range entering the loop, but seems to go out of range inside the while loop.
0

len returns the length, not the last index. If l=[1,2,3], then len(l) returns 3, but l[3] is out of range.

so you should use

while i<len(p)-1

or better yet:

for i in range(len(p)):

3 Comments

while i < len(p): p[i] is fine while i <= len(p): p[i] would throw and error. But @sqd's error is increment i++ in between the two loops, so it would be like the above but with p[i++]
This should be avoided in Python, if you really need to get at the numbers, use for i in enumerare(p)
well,if i=3,than i<3 is false.So the loop end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.