How to combine elements from two lists into a third?

前端 未结 6 1255
星月不相逢
星月不相逢 2020-12-06 04:09

I have two lists a and b:

a  =   [3,    6,   8,   65,   3]
b  =   [34,   2,   5,   3,    5]

c gets [3/34, 6/2, 8/5, 65/3, 3/5]
         


        
6条回答
  •  盖世英雄少女心
    2020-12-06 04:51

    You can do this using list comprehension (element by element):

    div = [ai/bi for ai,bi in zip(a,b)]
    

    Note that if you want float division, you need to specify this (or make the original values floats):

    fdiv = [float(ai)/bi for ai,bi in zip(a,b)]
    

提交回复
热议问题