Getting

前端 未结 4 1381
梦如初夏
梦如初夏 2020-12-16 20:56

I have 2 lists:

first_lst = [(\'-2.50\', 0.49, 0.52), (\'-2.00\', 0.52, 0.50)]
second_lst = [(\'-2.50\', \'1.91\', \'2.03\'), (\'-2.00\', \'1.83\', \'2.08\')         


        
4条回答
  •  自闭症患者
    2020-12-16 21:19

    You need to use tuple() or list() to convert that generator expression to a list or tuple:

    [tuple((fir[0], fir[1]*sec[1], fir[2]*sec[2]) for fir in first_lst)\
                                   for sec in second_lst if fir[0] == sec[0]]
    

    Working version of your code:

    >>> first_lst = [tuple(float(y) for y in x) for x in first_lst]
    >>> second_lst = [tuple(float(y) for y in x) for x in second_lst]
    
    >>> [((fir[0],) + tuple(x*y for x, y in zip(fir[1:], sec[1:]))) \
                      for fir in first_lst for sec in second_lst if fir[0]==sec[0]]
    [(-2.5, 0.9359, 1.0555999999999999), (-2.0, 0.9516000000000001, 1.04)]
    

提交回复
热议问题