import random
lists=[[150, 9, 'drop'], [35,13,"cage"],[200,153,"lust"],[160,50,"google"],[60,15,"gun"],[45,68,"razor"],[60,27,"armor"] ]
x=random.sample(lists, 3)
theitems=[]
for i in range(len(x)):
theitems.append(x[i][2])
print theitems
tw=0
tv=0
while True:
epilogi=raw_input("choose")
for i in range(len(theitems)):
if theitems[i]==epilogi:
tw=tw+x[i][1]
tv=tv+x[i][0]
theitems.remove(theitems[i])
x.remove(x[i])
print theitems,x
19 Replies - 767 Views - Last Post: 13 December 2012 - 10:20 AM
#1
why ? ( list index out of range )
Posted 12 December 2012 - 12:18 PM
Replies To: why ? ( list index out of range )
#2
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:20 PM
#3
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:23 PM
#4
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:25 PM
#5
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:28 PM
#6
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:29 PM
#7
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:32 PM
#8
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:36 PM
[0]
[1]
[2]
Right?
Your for loop says what? "Start at 0 and go to the array's length" right? If you said the length was 3 that means the values of i are: 0, 1, 2, 3. Is 3 a valid index of the array (above) in this example? No, no it is not. It is out of the bounds of the array.
#9
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:39 PM
modi123_1, on 12 December 2012 - 12:36 PM, said:
[0]
[1]
[2]
Right?
Your for loop says what? "Start at 0 and go to the array's length" right? If you said the length was 3 that means the values of i are: 0, 1, 2, 3. Is 3 a valid index of the array (above) in this example? No, no it is not. It is out of the bounds of the array.
so i should put range(len(theitems)-1) ?
would that work ?
#10
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:46 PM
for i in range(3):\ print i
prints 0, 1, 2, as you'd want, so that's fine.
I think the problem is that you're removing an item from the list as you iterate over it. That's called a concurrent modification, and it's probably what's causing your problem.
Let's walk through it. I have a list l=[1,2,3,4]. I want to remove even items from the list, so I walk through the list, and for each item in the list, I remove it if it's even.
for i in range (len(l)):
if l[i]%2==0:
l.remove(l[i])
What happens? Why? Hint: how many times is range(len(l)) evaluated?
In python, you seldom want to iterate over a list by index. Instead, just iterate over the items themselves:
for item in l: handle(item)
This post has been edited by jon.kiparsky: 12 December 2012 - 12:47 PM
#11
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:48 PM
@jon: pffssh.. I was going to get there.. (eventually)
This post has been edited by modi123_1: 12 December 2012 - 12:49 PM
#12
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:49 PM
jon.kiparsky, on 12 December 2012 - 12:46 PM, said:
for i in range(3):\ print i
prints 0, 1, 2, as you'd want, so that's fine.
I think the problem is that you're removing an item from the list as you iterate over it. That's called a concurrent modification, and it's probably what's causing your problem.
Let's walk through it. I have a list l=[1,2,3,4]. I want to remove even items from the list, so I walk through the list, and for each item in the list, I remove it if it's even.
for i in range (len(l)):
if l[i]%2==0:
l.remove(l[i])
What happens? Why? Hint: how many times is range(len(l)) evaluated?
oh you mean its because i change the length as i go on ?
so i would solve it by doing something like
o=len(list)
for i in range (o)?
#13
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:50 PM
fledgeling, on 12 December 2012 - 02:49 PM, said:
so i would solve it by doing something like
o=len(list)
for i in range (o)?
That's the problem, but that's not the solution.
Consider this:
>>> for i in l: ... if i%2==0: l.remove(i)
Try it in your interpreter and see if it works better than the other version.
#14
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:52 PM
jon.kiparsky, on 12 December 2012 - 12:50 PM, said:
fledgeling, on 12 December 2012 - 02:49 PM, said:
so i would solve it by doing something like
o=len(list)
for i in range (o)?
That's the problem, but that's not the solution.
Consider this:
>>> for i in l: ... if i%2==0: l.remove(i)
Try it in your interpreter and see if it works better than the other version.
Ok i will but i really must leave my computer now, i will test it later and post again , Thank you very much the help of both of you is apreciated
#15
Re: why ? ( list index out of range )
Posted 12 December 2012 - 12:55 PM
As you get the hang of this, you might find that it works better to do this in a list comprehension or a filter - those seem more "pythonic" approaches to this sort of problem - but for now, just getting away from the iteration by index will help a lot.
This post has been edited by jon.kiparsky: 12 December 2012 - 12:57 PM
|
|

New Topic/Question
Reply



MultiQuote






|