简体   繁体   English

如何在python中创建二维数组?

[英]How do I create a 2D array in python?

Problem: I'm trying to create an 2D array( like the last line in the code) using a for loop like the last line in the following matlab code:问题:我正在尝试使用 for 循环(如以下 matlab 代码中的最后一行)创建一个二维数组(如代码中的最后一行):

for i=1:NumLines
J=i-1;  J=0,1,2,...
if (mod(J,2)==0)
    %Even
    gj=gj_even;
else
    %Odd
    gj=gj_odd;
end
Erot(i)=((B0*J*(J+1))-D0*J^2*(J+1)^2)*planck*c; %correct

dnu_Stokes(i)=-B0*2*(2*J+3)+D0*(3*(2*J+3)+(2*J+3)^3); %2005
Xj_Stokes(i)=(J+1)*(J+2)/(2*J+3); %correct
dRCS_Stokes(i,:)=(112*pi^4/15).*(gj*planck*c*B0*(nu0+dnu_Stokes(i))^4*gammaSquared)./(((2*I+1).^2)*kb*T).*Xj_Stokes(i).*exp(-Erot(i)./(kb*T));

Heres my problem.Unlike matlab, python doesn't create a new array for you so you would have to create an empty array before you can place value insides them.这是我的问题。与 matlab 不同,python 不会为您创建新数组,因此您必须先创建一个空数组,然后才能将值放入其中。 I keep getting " only length-1 arrays can be converted to Python scalars" error .Here is my python attempt:我不断收到“只有长度为 1 的数组可以转换为 Python 标量”错误。这是我的 Python 尝试:

dnu_Stokes=np.array([])#empty arrays
Erot= np.array([])
Xj_Stokes=np.array([])
dRcs_Stokes=np.array([])
anti_dnu_Stokes=np.array([])
anti_Xj_Stokes=np.array([])
anti_dRcs_Stokes=np.array([]) #empty arrays

for i in range(1,NumLines):
    J= i-1
    if (J%2==0):
        gj=gj_even
    else:
        gj=gj_odd

    i_Erot = (B0*J*(J+1))-((D0*(J**2))*((J+1)**2)*planck*c)
    Erot= np.append(Erot,i_Erot) # append vaues to EROT array (WORKS)

    i_dnu_Stokes=-B0*2*(2*J+3)+D0*(3*(2*J+3)+(2*J+3)**3)
    dnu_Stokes= np.append(dnu_Stokes,i_dnu_Stokes)# append vaues to dnu_skotes array (wORKS)

    i_Xj_Stokes=(J+1)*(J+2)/(2*J+3)
    Xj_Stokes= np.append(Xj_Stokes,i_Xj_Stokes) # append vaues to XJ array (WORKS)


    dRcs_Stokes[i-1,:]=((112*((math.pi)**4))/15)*(((gj)*planck*c*B0*((nu0+i_dnu_Stokes)**4)*gammaSquared)/(((2*I+1)**2)*kb*T))*(i_Xj_Stokes*(math.exp(((-i_Erot)/(kb*T))))) ###I dont know how to append values to create a 2D array###

Here's a simple, "hello world" example of what you're trying to do:这是一个简单的“hello world”示例,说明您要执行的操作:

arr = []
for row_num in range(5):  # add 5 rows. row_num is 0...4
    row = []
    for col_num in range(6):  # add 6 columns. col_num is 0...5
        print("computing element for row:", row_num, ", col:", col_num)
        element = (row_num * col_num)  # this is where you compute each element
        row.append(element)
    arr.append(row)

arr = np.array(arr)

You should be able to take it from here你应该可以从这里拿走

Do you know (or can you calculate) the dimensions of your array, or the number of entries?你知道(或者你能计算出)数组的维度,或者条目的数量吗?

You can take a 1D array of some length and use reshape to change its dimensions.您可以采用一定长度的一维数组并使用reshape来更改其尺寸。

For instance, consider that you know ahead of time that you will store 100 values.例如,假设您提前知道将存储 100 个值。 You can preallocate an array of zeros, for example:您可以预先分配一个零数组,例如:

a = np.zeros(100)

And then reshape it any number of ways:然后以多种方式重塑它:

b = a.reshape(10, 10) # 10x10 2D array
c = a.reshape(2, 5, 10) # 2x5x10 3D array

etc.等等。

Indexing is as you would expect:索引如您所料:

print(b[0]) # array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
print(b[0][2]) # 0.0
print(c[1][3][8]) # 0.0

You can use indexing to set a value:您可以使用索引来设置一个值:

b[0][2] = 1.234
print(b[0]) # array([0.   , 0.   , 1.234, 0.   , 0.   , 0.   , 0.   , 0.   , 0.   ,       0.   ])

Using append will slow your program down a lot, if you are working with a large array.如果您正在使用大型数组,则使用append会大大降低您的程序速度。

Preallocation will save time and memory, and help you catch indexing bugs.预分配将节省时间和内存,并帮助您捕获索引错误。

Hopefully this helps in rethinking your problem.希望这有助于重新思考您的问题。

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

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