Python 在重复的情况下,只对列表的一个值求和,并将其转换为字典
斯塔克,你好 我有一个问题,我几乎解决了,但是,我不知道如何做最后一步 因此,在初始列表中有两个几乎相同的元素Python 在重复的情况下,只对列表的一个值求和,并将其转换为字典,python,python-3.x,list,dictionary,Python,Python 3.x,List,Dictionary,斯塔克,你好 我有一个问题,我几乎解决了,但是,我不知道如何做最后一步 因此,在初始列表中有两个几乎相同的元素('Tomato',3'PCS')和('Tomato',2'PCS'),其中只有中间参数(数量)不同,最后它应该是这两个数量的总和。最后我应该把3和2相加。我将描述,这将更容易理解: 因此,最初的清单是: initial = [('Egg', 2, 'PCS'), ('Milk', 100, 'ml'), ('Tomato', 3, 'PCS'), ('Beef', 500, 'g'),
('Tomato',3'PCS')
和('Tomato',2'PCS')
,其中只有中间参数(数量)不同,最后它应该是这两个数量的总和。最后我应该把3和2相加。我将描述,这将更容易理解:
因此,最初的清单是:
initial = [('Egg', 2, 'PCS'), ('Milk', 100, 'ml'), ('Tomato', 3, 'PCS'), ('Beef', 500, 'g'), ('Sweet pepper', 1, 'PCS'), ('Pita bread', 2, 'PCS'), ('Wine vinegar', 1, 'tbsp'), ('Tomato', 2, 'PCS')]
最后我应该得到:
{'Beef': {'measure': 'g', 'quantity': 500},
'Egg': {'measure': 'PCS', 'quantity': 2},
'Milk': {'measure': 'ml', 'quantity': 100},
'Pita bread': {'measure': 'PCS', 'quantity': 2},
'Sweet pepper': {'measure': 'PCS', 'quantity': 1},
'Tomato': {'measure': 'PCS', 'quantity': 5},
'Wine vinegar': {'measure': 'tbsp', 'quantity': 1}}
但在最后一行,我收到了“Tomato”:{'measure':'PCS','quantity':2}
,因为列表中的最后一个元素只是替换具有类似特征的元素,而不是求和“quantity”
我得到的结果是:
{'Beef': {'measure': 'g', 'quantity': 500},
'Egg': {'measure': 'PCS', 'quantity': 2},
'Milk': {'measure': 'ml', 'quantity': 100},
'Pita bread': {'measure': 'PCS', 'quantity': 2},
'Sweet pepper': {'measure': 'PCS', 'quantity': 1},
'Tomato': {'measure': 'PCS', 'quantity': 2},
'Wine vinegar': {'measure': 'tbsp', 'quantity': 1}}
我的代码是:
for elems2 in initial:
dinit = {'measure': elems2[2], 'quantity': elems2[1]}
d = {}.fromkeys([elems2[0]], dinit)
new_book[elems2[0]] = dinit
pprint(new_book)
有人能帮我解决列表元素内“数量”元素求和的问题吗?您可以使用一个简单的for循环:
result = {}
for item in initial:
key, quantity, measure = item
if key in result:
result[key]["quantity"] += quantity
else:
result[key] = {"measure": measure, "quantity": quantity}
print(result)
这就产生了
{'Egg': {'measure': 'PCS', 'quantity': 2},
'Milk': {'measure': 'ml', 'quantity': 100},
'Tomato': {'measure': 'PCS', 'quantity': 5},
'Beef': {'measure': 'g', 'quantity': 500},
'Sweet pepper': {'measure': 'PCS', 'quantity': 1},
'Pita bread': {'measure': 'PCS', 'quantity': 2},
'Wine vinegar': {'measure': 'tbsp', 'quantity': 1}}
你可以做:
from collections import defaultdict
import pprint
initial = [('Egg', 2, 'PCS'), ('Milk', 100, 'ml'), ('Tomato', 3, 'PCS'), ('Beef', 500, 'g'), ('Sweet pepper', 1, 'PCS'),
('Pita bread', 2, 'PCS'), ('Wine vinegar', 1, 'tbsp'), ('Tomato', 2, 'PCS')]
# count the measures by ingredient
counts = defaultdict(int)
for ingredient, quantity, measure in initial:
counts[(ingredient, measure)] += quantity
result = {ingredient: {'measure': measure, 'quantity': quantity} for (ingredient, measure), quantity in counts.items()}
pprint.pprint(result)
输出
{'Beef': {'measure': 'g', 'quantity': 500},
'Egg': {'measure': 'PCS', 'quantity': 2},
'Milk': {'measure': 'ml', 'quantity': 100},
'Pita bread': {'measure': 'PCS', 'quantity': 2},
'Sweet pepper': {'measure': 'PCS', 'quantity': 1},
'Tomato': {'measure': 'PCS', 'quantity': 5},
'Wine vinegar': {'measure': 'tbsp', 'quantity': 1}}
使用for循环有时间优势吗?对于这个特定的例子,可能没有,对于更大的数据可能没有。