繁体   English   中英

如何将两个列表相乘?

[英]How multiply two lists with each other?

帮助解决问题。 有一个包含两个表的数据库,例如:

表 1 - 价格

Base Metals 165.0
Condensates 130.0
Condensed Alloy 200.0

表 2 - 资源

Base Metals 8.04
Condensates 19.83
Condensed Alloy 30.21

我想将第二列中的所有值相乘并得到一个需要添加到基础 (SQLite) 中的列表,如下所示:

Base Metals 1326.6
Condensates 2577.9
Condensed Alloy 6042.0

我得到的最大值是列表中的 select 个值,以这种方式将它们添加到数组中:

prices_arr = []
planetary_arr = []

for row in cursor.execute("SELECT name, highest_buy FROM prices ORDER BY name"):
    prices_arr.append(row)

for row in cursor.execute("SELECT resource, mining_hour FROM planetary ORDER BY resource"):
    planetary_arr.append(row)

但是如何将值相乘并将名称留在列表中,我不明白。 尝试通过 NumPy,从 arrays 中删除“名称”,只留下值,但我不明白如何将它们与名称结合起来。

for row in cursor.execute("SELECT highest_buy FROM prices ORDER BY name"):
    prices_arr.append(row)

for row in cursor.execute("SELECT mining_hour FROM planetary ORDER BY resource"):
    planetary_arr.append(row)

result = np.multiply(prices_arr,planetary_arr)

Output

[[1326.6 ]
[2577.9 ]
[6042.  ]...
result = [(entry1[0], entry1[1] * entry2[1]) for entry1, entry2 in zip(prices_arr, planetary_arr)]

这就是您所要求的,但我认为使用类似于 @8hubham sugested 的字典可能是更好的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM