Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dividing list of values with factors in a dictionary list

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading