Ask Your Question
0

'list indices must be integers, not tuple' error in newbie script

asked 2013-06-05 22:41:32 +0200

mresimulator gravatar image

updated 2015-01-14 09:39:08 +0200

FrédéricC gravatar image

Hi expers!!

Im a new user of SAGE. Right now im writing a script that plot 'N' random lines of lenght=1 in a rectangle of b*h center in axes. The code is shhow below:

N=float(raw_input('Número de rectas aleatóreos (N)?:\n'))
b=float(raw_input('Base del rectángulo (b>1)?:\n'))
h=float(raw_input('Altura del cuadrado (h>1)?:\n'))

uniforme_x=RealDistribution('uniform',[-b/2,b/2])
uniforme_y=RealDistribution('uniform',[-h/2,h/2])

lista_x=[uniforme_x.get_random_element() for j in srange(N)]
lista_y=[uniforme_y.get_random_element() for j in srange(N)]

lista_xy = zip(lista_x , lista_y)

lineas=[]

d2=1*1*2

for j in lista_xy:
    x1 = lista_xy[j][0]
    y1 = lista_xy[j][1]
    while d2>1*1:
        x2 = uniforme_x.get_random_element()
        y2 = uniforme_x.get_random_element()
        d2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)
    lineas.append(line([lista_xy[j],(x2,y2)]))

grafico=sum(lineas)

grafico.show()

When i execute this code I get the error messege 'list indices must be integers, not tuple'.

Whats the problem?

Waiting for your answers.

Thanks a lot!

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2013-06-06 08:04:15 +0200

mresimulator gravatar image

Thanks a lot. Im a Python newbie too.

Best regards,

edit flag offensive delete link more
1

answered 2013-06-05 23:21:12 +0200

kcrisman gravatar image

This is a Python newbie error, not really a Sage error. Here is some sample code, where I've inputted 5, 4, and 3 for your raw_input things.

sage: lista_xy
[(0.790543686599, 0.496880250983),
 (-0.00532463751733, -0.648275931599),
 (1.5688782027, 0.968919148436),
 (-1.65441823658, 0.556830151705),
 (-0.270724125206, 1.49786789808)]

Note that each j in this lista is a tuple of floating point numbers. So you can't take the [j] index. If you remove the [j] pieces and use j itself, your code works fine:

for j in lista_xy:
    x1 = j[0]
    y1 = j[1]
    while d2>1*1:
        x2 = uniforme_x.get_random_element()
        y2 = uniforme_x.get_random_element()
        d2=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)
    lineas.append(line([j,(x2,y2)]))

Good luck!

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-06-05 22:41:32 +0200

Seen: 14,769 times

Last updated: Jun 06 '13