简体   繁体   中英

Loops with two arrays (or 2 dimension arrays) in python

I have two arrays that I want to work with and use in a calculation. The two arrays are:

 [50, 55, 60, 65, 70],
 [.00001, 10, 20, 30, 40]

I apply the the calculation, and I want the bottom array to give me answers across, and indexed by the top array. It should look something like this:

50     168.31     167.52     165.21     161.57     156.88   
55     168.01     167.33     165.51     141.57     155.88   
60     178.31     178.52     178.21     178.57     178.88   
65     188.31     188.52     188.21     188.57     188.88   
70     191.31     191.52     191.21     191.57     191.88   

I have tried several things including using the statement

if vel in   [50, 55, 60, 65, 70,]:
    print ( some formula in here)

and I tried putting in a second loop with the bottom array but of course it didn't work because the print had to be outside the first loop OR the second array would print every time the inside loop was executed.

How could I fix this program?

UPDATE

Sorry guys I tried to simplify it because it seamed like the arrays were to large in and of themselves to fit on the page and format correctly when put into the matrix. My fault... which I will attempt to fix now. Oh and this whole program supposed to calculate the maximum length of a wire antenna given the takeoff angle of the signal(theta) and the velocity factor of the wire used (velocity)

First I had been trying to use two separate arrays the first one is:

[50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100] <--- Velocity

and the second one is:

[.00001, 10, 20, 30, 40, 50, 60, 70, 80, 89.9999] <--- theta

Ok, now the two are related by the following equation: wire length= ("{:3.2f}".format(WL_feet(freq)/(4*(100/velocity)-(Hamath.cos_d(theta)))),end=" ")

I tried to put both arrays into a loop but like I said before the last print statement was in the wrong place and of course it didn't work,
something like this:

for velocity in [50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]:

print(" ",end=" ")

`for theta in [.00001, 10, 20, 30, 40, 50, 60, 70, 80, 89.9999]:`

    `print ("{:3.2f}".format(WL_feet(freq)/(4*100/velocity),           -(Hamath.cos_d(theta)))),end=" ")`

`print(velocity)`

Unfortunately the editor changes the format of the text and messes up the white space and and flow making it harder read. I tried to use the editor to help format the program text.

So again I want the type of matrix shown above but of course with the entire contents of both arrays.

Hope this helps,
Poe

The array you have written is a 5 by 2 array, it is two dimensional. I think an easier way to do this problem is to have two different arrays, rather than one two dimensional array. I also don't think you can end up with a 5 by 6 array with only two 5 by 1 arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM