【问题标题】:Calculate product of index values in list of list in python [duplicate]计算python列表列表中索引值的乘积[重复]
【发布时间】:2017-12-12 05:45:07
【问题描述】:

给定一个频率列表(参见示例)。如何获得列表中所有第 n 个成员的乘积。 例如:

l1 = [1,2,3]
l2=[4,5,6] 
output = [4,10,18] 

如果列表的长度未知,我该如何做到这一点。请用pythonic的方式回答,不胜感激!

样本数据:

[
 [0.8507462686567164, 0.14925373134328357], [0.9870967741935484, 
 0.012903225806451613], [0.5833333333333334, 0.4166666666666667], 

 [0.4791666666666667, 0.5208333333333334], [0.9379084967320261, 
 0.06209150326797386], [0.9657320872274143, 0.03426791277258567], 

 [0.9914529914529915, 0.008547008547008548], [0.9050279329608939, 
 0.09497206703910614], [0.7660944206008584, 0.23390557939914164]
]

输出:

[product of 1st elements, product of 2nd elements, product of n elements]

【问题讨论】:

  • 使用zip函数。
  • 进行所需的长度检查或错误检查,您应该可以执行以下操作[a*b for a,b in zip(l1,l2)]
  • print([x * y for x, y in zip(l1, l2)].

标签: python list dictionary


【解决方案1】:

你可以试试:

>>> l1 = [1,2,3]
>>> l2=[4,5,6]
>>> [x * y for x,y in zip(l1,l2)]
[4, 10, 18]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2014-07-01
    • 2014-07-29
    相关资源
    最近更新 更多