简体   繁体   English

用于创建2D数组形式2 2D数组的python for循环

[英]python for loop for creating a 2D array form 2 1D array

I want to use python for getting vibrational partition function at different temperature. 我想使用python在不同温度下获得振动分区功能。 I have two 1D array like: freq=[100,150,200] (This is actually read form a .dat file) and Temp=np.arange(273.,973.,25) . 我有两个一维数组,例如: freq=[100,150,200] (实际上是从.dat文件读取的)和Temp=np.arange(273.,973.,25) Now, qvib=product over freq for a particular temperature (1/1-exp(-h*nu/kb/T)) . 现在,对于特定温度(1/1-exp(-h*nu/kb/T)) ,qvib =超过频率的乘积。 My for loop is: 我的for循环是:

for i in range(len(T)):
 for j in range(len(freq)):
  q[i][j]=1/(1-np.exp(-h*freq[j]/kb/T[i]))
   Q[i]=np.prod(q[i][:])

I am using python for two to three days only. 我只用python两到三天。 I even don't know all the in line operation of python. 我什至不知道python的所有内联操作。 I have two question: 1) Can i do it without using for loop (like using only sum or no.prod) 2) My for loop is not working. 我有两个问题:1)我可以不使用for循环来做到这一点(例如仅使用sum或no.prod)2)我的for循环无法正常工作。 I searched on internet but couldn't found any good for loop reference for this type of mathematical operation. 我在互联网上搜索,但找不到此类数学运算的循环参考有什么用。 Thanks in advance for your kind help. 在此先感谢您的帮助。

You need to at least define Q as an empty array the same length as your temperature array before you can assign to it.: 您必须至少将Q定义为与温度数组长度相同的空数组,然后才能分配给它:

Q = np.empty(len(T))

Better is: 更好的是:

for i in range(len(T)):
    q = 1/(1-np.exp(-h*freq/kb/T[i]))     
Q = np.prod(q)

But the really nice thing about NumPy is the ability to do away with Python loops altogether: 但是,关于NumPy的真正好处是可以完全消除Python循环:

Q = np.prod(1/(1-np.exp(-h*freq[:, None]/kb/T)), axis=0)

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

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