problem in reading indices

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Thu Dec 13 08:25:21 EST 2007


Xavier Barthelemy a écrit :
> Hi all
> 
> I'm becoming mad, because I can't see what's wrong:
> 
> I am constructing a GUI, to plot some data.
> so let's have a look of what's wrong:
> 
> 
> 
> in my code I have a variable named choice[i].current which is the
> current selection of the i-th Listbox object. it is a tuple, with one
> element.
> 
> so when I write
> 
> print type(i),type(choice[i].current)
> I have: int and tuple
> 
> print type(i),type(choice[i].current[0])
> I have: int and str
> 
> print type(i),type(int(choice[i].current[0]))
> I have: int and int
> 
> so when I call another array with these indices
> ArrayWithData[i,int(choice[i].current[0])]
> 
> I have the following error: TypeError: list indices must be integers

the syntax for list subscripting is:

   thelist[index]

not:

   thelist[index,index]


If the item at thelist[index] is itself an object that supports 
subscripting and you want to subscript it to (which seems to be the case 
here), the syntax is:

   thelist[index][subindex]

IOW, try with:

   ArrayWithData[i][int(choice[i].current[0])]

> so I tried an intermediate value, because sometimes, the oneliner code
> doesn't work,

if you're sure that choice[i].current[0] exists and can be passed to the 
int type, there's no reason for the oneliner to behave differently.

HTH



More information about the Python-list mailing list