简体   繁体   English

python中具有两个数组(或2维数组)的循环

[英]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 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 [50,55,60,65,70,75,80,85,90,95,100] <---速度

and the second one is: 第二个是:

[.00001, 10, 20, 30, 40, 50, 60, 70, 80, 89.9999] <--- theta [.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=" ") 好的,现在两者通过以下方程式关联:电线长度=(“ {: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. 您编写的数组是5 x 2数组,是二维的。 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. 我也不认为您最终只能得到5 x 6的阵列,而只有两个5 x 1的阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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