簡體   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