【发布时间】:2021-02-10 17:41:47
【问题描述】:
我正在尝试构建一个程序,该程序将采用 [int,int] 或键/值对的嵌套列表,并与另一个 dict 的键/val 匹配,并将第二个中的值添加到在启动的 int 变量中0.
from collections import Counter
shoe_sizes = [2,3,4,5,6,8,7,6,5,18]
shoe_collection = Counter(shoe_sizes)
customer_wants = [[6,55],[6,45], [6,55], [4,40], [18,60], [10, 50]]
income = 0
for i in customer_wants:
if i in shoe_collection:
income += dict[customer_wants[i][1]]
print(income)
shoe_collection 中的键是鞋码,值是可用的数量。
我在这里要做的是获取customer_wants 中的每个嵌套列表,并检查shoe_collection 中的第一个元素(鞋码)是否可用。如果是这样,我想在customer_wants 中添加该嵌套列表的第二个元素(价格)并将其添加到income。
使用两个字典而不是将customer_wants 作为嵌套列表会更容易吗?我尝试比较 list-dict 和 dict-dict 但得到的 TypeErrors 都不是可散列的。谢谢!
【问题讨论】:
标签: python arrays dictionary