填二维列表与值而不是零第一初始化它 [英] Fill two-dimensional list with values instead of initializing it first with zeros

查看:116
本文介绍了填二维列表与值而不是零第一初始化它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想与重新present权力价值,填补了一个二维数组,但我的问题就出在code的速度,因为二维数组是100x100的大小和我不'T想先用zereos的100x100的名单初始化它然后填写该名单与价值观,而是直接填写同比值100x100的二维列表。我的code所示向下跌破

I have a two-dimensional array that I want to fill up with values that represent powers but my problem lies in the speed of the code because the two-dimensional array is 100x100 size and I don't want to first initialize it with 100x100 list of zereos then fill up the list with values but rather fill up the 100x100 two-dimensional list by values directly. My code is shown down below

x_list = np.linspace(min_x, max_x, (max_x - min_x)+1)
 y_list = np.linspace(min_y, max_y, (max_y - min_y)+1)

X, Y = np.meshgrid(x_list, y_list)
Y = Y[::-1]
Z = [[0 for x in range(len(x_list))] for x in range(len(y_list))]         #Z is the two-dimensional list containing powers of reach position in the structure to be plotted

for each_axes in range(len(Z)):
    for each_point in range(len(Z[each_axes])):
        Z[len(Z)-1-each_axes][each_point] = power_at_each_point(each_point, each_axes)
#The method power_at_each_point is the one that calculates the values in the two-dimensional array Z

我想要做的一个例子是,而不是做什么如下所示:

An example what I want to do is instead of doing what is shown below:

Z_old = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
for each_axes in range(len(Z_old)):
    for each_point in range(len(Z_old[each_axes])):
        Z_old[len(Z_old)-1-each_axes][each_point] = power_at_each_point(each_point, each_axes)

我现在想不初始化Z_old阵列零,而是用值填满它,同时通过它遍历这将是类似的下面写虽然它的语法是可怕的错误,但是这就是我想要的,达到什么结束。

I want now to not initialize the Z_old array with zeroes but rather fill it up with values while iterating through it which is going to be something like the written below although it's syntax is horribly wrong but that's what I want to reach in the end.

 Z = np.zeros((len(x_list), len(y_list))) for Z[len(x_list) -1 - counter_1][counter_2] is equal to power_at_each_point(counter_1, counter_2] for counter_1 in range(len(x_list)) and counter_2 in range(len(y_list))]

加如下所示,如果它可以帮助你明白我想要做它的相关方法power_at_each_point的方法:

plus the method of power_at_each_point is shown below with it's related methods if it helps you understand what I wanted to do:

 #A method to calculate the power reached from one node to the other for contourf function

def cal_pow_rec_plandwall_contour(node_index_tx, receiver):   
nodess_excel = xlrd.open_workbook(Node_file_location)
nodes_sheet = nodess_excel.sheet_by_index(0)

node_index_tx_coor = [nodes_sheet.cell_value(node_index_tx - 1, 3), nodes_sheet.cell_value(node_index_tx - 1, 4)] #just co-ordinates of a point
distance = cal_distance(node_index_tx_coor, receiver)
if distance == 0:
    power_rec = 10 * (np.log10((nodes_sheet.cell_value(node_index_tx - 1, 0) * 1e-3)))
    return power_rec  #this is the power received at each position
else:
    power_rec = 10 * (np.log10((nodes_sheet.cell_value(node_index_tx - 1, 0) * 1e-3))) - 20 * np.log10((4 * math.pi * distance * 2.4e9) / 3e8) - cal_wall_att([node_index_tx_coor, receiver])
    return power_rec


def power_at_each_point(x_cord, y_coord):  #A method to get each position in the structure and calculate the power reached at that position to draw the structure's contourf plot
fa = lambda xa: cal_pow_rec_plandwall_contour(xa, [x_cord, y_coord])
return max(fa(each_node) for each_node in range(1, len(Node_Positions_Ascending) + 1)) #Node_position_ascending is a list containing the co-ordinate positions of markers basically or nodes.

如果有人能告诉我怎样才能填补二维数组Z,使用值从顶部底部像我一样在那里没有最初设定的二维数组第一个零这将是更AP preciated。

If someone could tell me how can I fill the two-dimensional array Z with values from the bottom of the top as I did right there without initially setting the two-dimensional array to zero first it would be much appreciated.

推荐答案

确定,首先,你要创建一个numpy的阵列,而不是一个名单列表。这几乎总是会显著小,快一点去努力。而且,更重要的是它开启了大门,向量化的循环,这使得他们的很多的速度上下工夫。所以,与其这样:

OK, first, you want to create a NumPy array, not a list of lists. This is almost always going to be significantly smaller, and a little faster to work on. And, more importantly, it opens the door to vectorizing your loops, which makes them a lot faster to work on. So, instead of this:

Z_old = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]

...做到这一点:

… do this:

Z_old = np.zeros((3, 5))

但是,现在让我们看看我们是否可以向量化的循环,而不是修改的值:

But now let's see whether we can vectorize your loop instead of modifying the values:

for each_axes in range(len(Z_old)):
    for each_point in range(len(Z_old[each_axes])):
        Z_old[len(Z_old)-1-each_axes][each_point] = each_point**2 + each_axes**2

z的初始值[...] 不被所有这里使用的,所以我们不需要用$ 0 P $ P-填充它们,就像你怀疑。什么的的每个点正在使用的研究 C 。 (我要重命名 Z_old each_axes each_point 以Z 研究 C 的简洁。)特别是,你要设置每个 Z [LEN(Z)-1-R,C] R ** 2 + C ** 2

The initial values of Z[…] aren't being used at all here, so we don't need to pre-fill them with 0, just as you suspected. What is being used at each point is r and c. (I'm going to rename your Z_old, each_axes, and each_point to Z, r, and c for brevity.) In particular, you're trying to set each Z[len(Z)-1-r, c] to r**2 + c**2.

首先,让我们扭转底片所以你每个 Z [R,C] 来设置的东西,在这种情况下,(LEN (Z)-1-R)** 2 + C ** 2

First, let's reverse the negatives so you're setting each Z[r, c] to something—in this case, to (len(Z)-1-r)**2 + c**2.

这东西只是一个关于研究 C 值功能。我们可以通过创建人气指数取值得到。尤其是,人气指数(5)仅仅是数字 0,1,2,3,4 的数组,和人气指数(5)** 2 是正方形的数组 0,1,4,9,16

That "something" is just a function on r and c values. Which we can get by creating aranges. In particular, arange(5) is just an array of the numbers 0, 1, 2, 3, 4, and arange(5)**2 is an array of the squares 0, 1, 4, 9, 16.

唯一的问题是,得到一个3x5的阵列离开这里,我们要的elementwise增加两个二维数组,一个3X1阵列和阵列1X5,反之亦然,但我们已经从<$ C有两个一维数组$ C>人气指数。好了,我们可以重塑其中之一:

The only problem is that to get a 3x5 array out of this, we have to elementwise add two 2D arrays, a 3x1 array and a 1x5 array, vice-versa, but we've got two 1D arrays from arange. Well, we can reshape one of them:

Z_old = (3 - 1 - np.arange(3))**2 + (np.arange(5)**2).reshape((5, 1))

您可以的,当然,这简化进一步(你显然不需要 3 - 1 ,你可以只需添加一个新的轴心没有重塑),但希望这直接说明它是如何对应到原来的code。

You can, of course, simplify this further (you obviously don't need 3 - 1, and you can just add a new axis without reshape), but hopefully this shows directly how it corresponds to your original code.

这篇关于填二维列表与值而不是零第一初始化它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆