problem in reading indices

X

Xavier Barthelemy

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.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.current)
I have: int and tuple

print type(i),type(choice.current[0])
I have: int and str

print type(i),type(int(choice.current[0]))
I have: int and int

so when I call another array with these indices
ArrayWithData[i,int(choice.current[0])]

I have the following error: TypeError: list indices must be integers

so I tried an intermediate value, because sometimes, the oneliner code
doesn't work, so with an intermediate passage:
value=int(choice.current[0])
ArrayWithData[i,value]

I have the same error

and I don't understand why. What's wrong?
May anyone have an idea?
Xavier

pm:
and print type(ArrayWithData), ArrayWithData gives me
<type 'list'> [array([ 2.01, 5.01]),...]
 
B

Bruno Desthuilliers

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.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.current)
I have: int and tuple

print type(i),type(choice.current[0])
I have: int and str

print type(i),type(int(choice.current[0]))
I have: int and int

so when I call another array with these indices
ArrayWithData[i,int(choice.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[int(choice.current[0])]
so I tried an intermediate value, because sometimes, the oneliner code
doesn't work,

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

HTH
 
B

Bjoern Schliessmann

Xavier said:
so when I call another array with these indices
ArrayWithData[i,int(choice.current[0])]


You don't "call" "arrays" "with indices". You are indexing the list
ArrayWithData using the index "i,int(blah)" which is invalid.
Indices must be integers, not comma seperated values.
so I tried an intermediate value, because sometimes, the oneliner
code doesn't work, so with an intermediate passage:
value=int(choice.current[0])
ArrayWithData[i,value]

I have the same error

and I don't understand why. What's wrong?
May anyone have an idea?


I'm afraid not! You didn't specify at all what you'd like to achieve
(just "it doesn't work! why?), and clairvoyance isn't as easy as it
looks.

If you want to slice, don't use Perl syntax. Python uses a colon for
index separation to eliminate ambiguities.

some_elements = my_list[3:10]

Regards,


Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top