从其余数据中减去第一个数据

Subtracting the first data off the rest

为了让 isotope['State_2_complete']isotope['State_640_complete'] 的值为:

,我可以在下面的函数中添加什么

isotope['State_2_complete'] - isotope['State_1_complete'],
isotope['State_3_complete'] - isotope['State_1_complete'],
isotope['State_4_complete'] - isotope['State_1_complete'],
... isotope['State_640_complete'] - isotope['State_1_complete'] ?

我认为这会很简单:

isotope['State_{0}_complete'.format(i)] = isotope['State_{0}_complete'.format(i)] - isotope['State_1_complete']

或类似的东西:

isotope['State_{0}_complete'.format(i)] -= isotope['State_1_complete']

但是这两个选项都只减去 isotope['State_1_complete'] - isotope['State_1_complete'] 其中我的 isotope['State_1_complete'] (我想要)得到 0 个值,但其余状态(例如 isotope['State_2_complete'], isotope['State_3_complete'], ... isotope['State_640_complete']) 保持原值。

def create_relative_excitation_energies_66_x_640_matrix(isotope, data_tbd, data_occ):
    """
    creates a 66 x 640 matrix by subtracting off the 
    ground state data from the excited states
    """

    # counter
    a = 10
    b = 12
    # getting our 640 states in numpy array form
    for i in range (1,641):
        isotope['State_{0}_inc'.format(i)] = np.genfromtxt(data_tbd, names=('a', 'b', 'c', 'd', 'J', 'T', 'X_JT(ab,cd)'),
                                        skip_header=a, max_rows=63, usecols=6)
        
        isotope['State_{0}_inc'.format(i)] = np.expand_dims(isotope['State_{0}_inc'.format(i)], axis=1)
                                        
        isotope['State_{0}_inc'.format(i)] = isotope['State_{0}_inc'.format(i)].astype('float64')
                                             
        isotope['State_{0}_spe'.format(i)] = np.genfromtxt(data_occ, names=('Particle', 'occ', 'Orbit_1', 'Orbit_2', 'Orbit_3'),
                                        skip_header=b, max_rows=2, usecols=(2,3,4))
        
        isotope['State_{0}_spe_sum'.format(i)] = np.vstack((isotope['State_{0}_spe'.format(i)]['Orbit_1'].sum(),
                                        isotope['State_{0}_spe'.format(i)]['Orbit_2'].sum(), isotope['State_{0}_spe'.format(i)]
                                        ['Orbit_3'].sum()))
        
        isotope['State_{0}_complete'.format(i)] = np.vstack((isotope['State_{0}_inc'.format(i)], isotope['State_{0}_spe_sum'.format(i)]))

####### isotope['State_{0}_complete'.format(i)] = *add code here* ###########
     
        a += 66
        b += 3
    
    matrix = np.hstack(([isotope[f'State_{i+1}_complete'] for i in range(640)]))
    
    return matrix

目前,您正在这样做:

isotope['State_{0}_complete'.format(i)] = np.vstack((isotope['State_{0}_inc'.format(i)], isotope['State_{0}_spe_sum'.format(i)]))

但是你说你想更新这些值,方法是从它本身和所有其他值中减去第一个这样的值。因此,您可以将上面的替换为:

if first is None:
    first = np.vstack((isotope['State_{0}_inc'.format(i)], isotope['State_{0}_spe_sum'.format(i)]))
isotope['State_{0}_complete'.format(i)] = np.vstack((isotope['State_{0}_inc'.format(i)], isotope['State_{0}_spe_sum'.format(i)])) - first

并在循环外和循环前定义first = None。由于 first 在第一次通过时只会是 None,那是它获得其值的时间,并且对于这些值的每次赋值,都会从中减去。