1

OK python 2 dimensional arrays (sorry i mean lists) are getting to me.

The code below will calculate the coordinates of the circumference of a circle in 0.1 degree steps. and i want to store it in a 2d array that ideally would have the index values 0,1,2,3... 3599, and each index position would contain the degree, and the x and y coordinate.

I get arrays in other languages but i know in python the syntax is different, and for some reason I just can't get my head around it. I have tried appending or as below trying to initialise the array, and always an error.

so
[0][0] = 0 degrees [0][1] = 2.0 [0][02] = 0
[1][0] = 0.1 degrees [1][1] = 1.99999695383 [1][2] = 0.0034906567318
Etc

I will need to do some more stuff with lists but at the moment just creating a 2D list both fully initialised and to be adjusted at run time would be a nice start. So if some one could correct the syntax on this for me and explain where I am wring I would be grateful.

Step = 0.1 #steps in degrees
    diamator = 2 #diamator of the circle
    degree = 0 # starting degreee
    Circlearray=[3600][2]
    #360 degrees devided by 0.1 (step) = 3600 so run the loop this many times
    for num in range(0,3600):
        #math functions work in rads so do the conversion
        rad = degree * (math.pi/180)
        #calculate the x,y cords for each value of degree
        x = 2*math.cos(rad)
        y = 2*math.sin(rad)
        #incress degree by 0.1 
        degree = degree + Step
        #save degree,x,y coordinates to array with a key of num 
        Circlearray[num][0] = degree
DevilWAH
  • 2,553
  • 13
  • 41
  • 57
  • 1
    What do you think `Circlearray=[3600][2]` will do? That is trying to index a third element in a list with a single element i.e 3600 – Padraic Cunningham May 09 '16 at 21:47
  • Padaric, to be honest that is only one thing i tried and possible not the most cleaver. I was just thinking how you declare an array in some thing like VB. dim array-name (10,2) is a array 10 by 2. And pretty much most of the other coding i have done seems to treat multi dimensional arrays in the same way as 1D. – DevilWAH May 09 '16 at 21:57
  • Maybe numpy might suit your needs better http://www.numpy.org/ – Padraic Cunningham May 09 '16 at 21:58
  • Looking at the question that is a possible duplicate, is that saying you basically create every element of the list list = " [[0 for x in range(w)] for y in range(h)] " as this is creating a list of lists, not what i would understand as an array. – DevilWAH May 09 '16 at 22:03
  • 1
    Like you say numpy might be what I am looking for. thanks for the pointers. it was suggested i should use Python as the graphics would be easier on a raspberry pi, but i am wondering now :) – DevilWAH May 09 '16 at 22:05

0 Answers0