Home > Enterprise >  how to change the list i'm saving my values after each loop?
how to change the list i'm saving my values after each loop?

Time:03-05

what i want is: when k=0,do all the mathematical stuff for all values of Qs and save it in a list. After that, do the same thing for k=1 and save it in another list etc...

Qs = [1,2,3,4,5,6,7,8,9,10]
m =[(10**(-2)), (10**(-2)), (10**(-2)), 1.27] 
e = [2/3, -1/3, -1/3, 2/3] 

psi_T = []
psi_L = []
for i in range(len(m)):
    psi_t = 1   m[i]
    psi_l = 2   e[i]
    psi_T.append(psi_t)
    psi_L.append(psi_l)



for k in range(len(psi_T)):
    sigmaT_up = [] 
    sigmaL_up = [] 
    for n in range(len(Qs)):    
        sigmat_up = 2*psi_T[k]   Qs[n]
        sigmal_up = 2*psi_L[k]   Qs[n]
        sigmaT_up.append(sigmat_up)
        sigmaL_up.append(sigmal_up)

    F2up = []  
    for n in range(len(Qs)): 
        F2u = (sigmaT_up[n]   sigmaL_up[n])
        F2up.append(F2u)

print(F2up)
[11.873333333333333, 13.873333333333333, 15.873333333333331, 17.87333333333333, 19.87333333333333, 21.87333333333333, 23.87333333333333, 25.87333333333333, 27.87333333333333, 29.87333333333333]

in my code, it keeps replacing the previous values ​​by the values ​​obtained with k=3, (k = 0,1,2,3). I know i gave to the code just one save list (F2up), but i have no clue of how to make it change the save list after each loop.

CodePudding user response:

Seems like you want to create a "master" list where master_list[i] is the F2up list created when k = i.

Qs = [1,2,3,4,5,6,7,8,9,10]
m =[(10**(-2)), (10**(-2)), (10**(-2)), 1.27] 
e = [2/3, -1/3, -1/3, 2/3] 

psi_T = []
psi_L = []
for i in range(len(m)):
    psi_t = 1   m[i]
    psi_l = 2   e[i]
    psi_T.append(psi_t)
    psi_L.append(psi_l)

# create master_list
master_list = []
for k in range(len(psi_T)):
    sigmaT_up = [] 
    sigmaL_up = [] 
    for n in range(len(Qs)):    
        sigmat_up = 2*psi_T[k]   Qs[n]
        sigmal_up = 2*psi_L[k]   Qs[n]
        sigmaT_up.append(sigmat_up)
        sigmaL_up.append(sigmal_up)

    F2up = []  
    for n in range(len(Qs)): 
        F2u = (sigmaT_up[n]   sigmaL_up[n])
        F2up.append(F2u)
    # add kth F2up list to master_list
    master_list.append(F2up)

print(master_list)
  • Related