Dividing list of values with factors in a dictionary list

Issue

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]

Solution

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.]]

Answered By – jezrael

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published