Home > Mobile >  Dividing list of values with factors in a dictionary list
Dividing list of values with factors in a dictionary list

Time:06-10

I have a dictionary as below.

fact_dic={'01':[0.5,0.2],
          '02':[0.4,0.2],
          '03':[0.45,0.3]}

And I have a list as below.

A=[100,200,300]

All I want is to divide each item in list A to its corresponding list items present in the dictionary fact_dict.

EG:

new_list1 =[50,20]
new_list2=[80,40]

CodePudding user response:

Pandas solution:

A=[100,200,300]
df = pd.DataFrame(fact_dic).mul(A)
    
print (df)
     01    02     03
0  50.0  80.0  135.0
1  20.0  40.0   90.0

Numpy solution:

A=[100,200,300]
a = np.array(list(fact_dic.values())) * np.array(A)[:, None]
    
print (a)
[[ 50.  20.]
 [ 80.  40.]
 [135.  90.]]
  • Related