i need help in 2d array !

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 3969 Views - Last Post: 05 December 2010 - 06:02 PM Rate Topic: -----

#1 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

i need help in 2d array !

Posted 04 December 2010 - 12:45 PM

Hello everyone !
I have difficulties with python.I hope I'm going to learn fast : )
my question is how to do 2D array in python ?
I knew that it's in java as that
a[][]=newdouble[n][m]
but I don't know what is it in python ?


from pylab import*

x_start = -200
x_end = 200
y_start = -100
y_end = 100
delta = 10
n = 40
m = 20
xi = 0
for xi in range(0,n+1):
  xi=x_start+(xi*delta)
  print "xi",xi
yi = 0
for yi in range(0,m+1):
  yi=y_start+(yi*delta)
  print "yi",yi


I'm trying to measure side of cube .
the x and y of the top side .
and I collect all the x direction I meant from (-200,200)
and the y form (-100,100)
I want to do 2D array to calculate all the point in the surface " top side "

need your Help !
layo

Is This A Good Question/Topic? 0
  • +

Replies To: i need help in 2d array !

#2 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: i need help in 2d array !

Posted 04 December 2010 - 12:52 PM

In Python, you can think of 2D arrays as a list of lists instead. In When you cycle through them, it is sort of the same time, but it is more dynamic than C/C++/Java arrays are. Here is how I would make a 5 by 5 array of numbers:

arr = [[1, 2, 3, 4, 5],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8],
       [5, 6, 7, 8, 9]]



Now, the formatting does not matter that much, but I'm just showing you how they work and making it simple to read. You can iterate through them like so:

for list in arr:
    for elem in list:
        print elem


Was This Post Helpful? 0
  • +
  • -

#3 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

Re: i need help in 2d array !

Posted 04 December 2010 - 01:02 PM

thanks for your help !
but the problem is that I don't know how to do a 2 arrays for 2 lists that I calculated.

This post has been edited by layo: 04 December 2010 - 01:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: i need help in 2d array !

Posted 04 December 2010 - 01:17 PM

Just cycle through and add lists to the main list. That's the advantage of having a list of lists. If you have another list, just add it to the main collector list. If you want to append a list to another, use the append() method:
>>> arr = []
>>> arr.append([6, 3.14, "Hi"])
>>> arr
[[6, 3.14, 'Hi']]
>>> arr.append(["another", "list", 43])
>>> arr
[[6, 3.14, 'Hi'], ['another', 'list', 43]]


Was This Post Helpful? 0
  • +
  • -

#5 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

Re: i need help in 2d array !

Posted 04 December 2010 - 02:19 PM

I understood what you said but I didn't know how to apply it in python.
xi = [-200,-190,-180 ......... ,200]
yi = [-100,-90,-80,.............100]

how can i get a list by array as that
a = [(-200,-100),(-190,-90),(-180,-80)............(200,100)]

I really try but i can't figure out !

This post has been edited by layo: 04 December 2010 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

Re: i need help in 2d array !

Posted 04 December 2010 - 04:47 PM

there is no any response for my last question ?!?!
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: i need help in 2d array !

Posted 04 December 2010 - 08:10 PM

That's really just a 1D array of points, isn't it?

Perhaps:
>>> xi = [-200,-190,-180 ,200]
>>> yi = [-100,-90,-80,100]
>>> a = [ ( xi[i], yi[i]) for i in range(len(xi)) ]
>>> a
[(-200, -100), (-190, -90), (-180, -80), (200, 100)]
>>>


Was This Post Helpful? 1
  • +
  • -

#8 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

Re: i need help in 2d array !

Posted 05 December 2010 - 12:46 PM

it didn't work ! it said " TypeError: object of type 'int' has no len()"

any idea ?
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: i need help in 2d array !

Posted 05 December 2010 - 12:50 PM

You need to do it like this:
>>> xi = [-200,-190,-180 ,200]
>>> len(xi)
4



Not like this:
>>> len(xi[1])

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    len(xi[1])
TypeError: object of type 'int' has no len()


Was This Post Helpful? 0
  • +
  • -

#10 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

Re: i need help in 2d array !

Posted 05 December 2010 - 12:58 PM

i want to take the first point in xi and the first point in yi
I meant
xi = [-200,-190,-180 ......... ,200]
yi = [-100,-90,-80,.............100]

how can i get a as that :
a = [(-200,-100),(-190,-90),(-180,-80)............(200,100)] ?
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: i need help in 2d array !

Posted 05 December 2010 - 01:13 PM

The code that baavgai works for me...see?
>>> xi = [-200,-190,-180,200]
>>> yi = [-100,-90,-80, 100]
>>> a = [(xi[i], yi[i]) for i in range(len(xi))]
>>> a
[(-200, -100), (-190, -90), (-180, -80), (200, 100)]


Was This Post Helpful? 0
  • +
  • -

#12 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

Re: i need help in 2d array !

Posted 05 December 2010 - 01:18 PM

i got as that

    a = [(xi[i], yi[i]) for i in range(len(xi))]
IndexError: list index out of range




i don't know why ?
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: i need help in 2d array !

Posted 05 December 2010 - 01:28 PM

Clearly your arrays are not equal.

Options include:
>>> 
>>> xi = [-200,-190,-180 ,200]
>>> yi = [-100,-90,-80]
>>> 
>>> # drop the rest of the longer
... [ ( xi[i], yi[i]) for i in range(min(len(xi),len(yi))) ]
[(-200, -100), (-190, -90), (-180, -80)]
>>> 
>>> # show default
... def v(a,i):
...     if i<len(a):
...             return a[i]
...     else:
...             return -99999
... 
>>> [ ( v(xi,i), v(yi,i)) for i in range(max(len(xi),len(yi))) ]
[(-200, -100), (-190, -90), (-180, -80), (200, -99999)]
>>> 


Was This Post Helpful? 0
  • +
  • -

#14 layo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 10

Re: i need help in 2d array !

Posted 05 December 2010 - 01:39 PM

from pylab import*




x_start = -200
x_end = 200
y_start = -100
y_end = 100
z_start = 100
z_end = 300
delta = 10
segma0 = 0.002
segma1 = 0.05
r_s = (50,-50,0)
I = 1
n = 40
m = 20
w = 20

xi=0
for xi in range(0,n+1):
  xi=x_start+(xi*delta)
  print xi
yi = 0
for yi in range(0,m+1):
  yi=y_start+(yi*delta)
  print yi
zi = 0
for zi in range(0,w+1):
  zi=z_start+(zi*delta)
  print zi


i'm doing in the xi and yi points
try it in my program , it doesn't work : (

This post has been edited by layo: 05 December 2010 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: i need help in 2d array !

Posted 05 December 2010 - 03:12 PM

Let me take one of those bits.

(x_start, x_end, n) = ( -200, 200, 40 )
delta = 10

# xi = 0 # this is pointless
for xi in range(0,n+1):
	xi = x_start + (xi*delta)
	print xi



Where are the arrays? Can you make an array out of all those values? You really aren't using x_end in any way.

Honestly, it's as simple as: range(-200, 201, 10). Work from there.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2