简体   繁体   English

删除重复项并在python中添加相似项

[英]remove duplicated items and add similar items in python

i created a dictionary that will take one key and two values, the dictionary will work as follows:我创建了一个字典,它将采用一个键和两个值,该字典将按如下方式工作:

  1. user inputted an item (item will be the key)用户输入了一个项目(项目将是关键)
  2. user chose how many quantity (value1)用户选择了多少数量(value1)
  3. price (value2)价格(价值2)

what am trying to approach is I want to add all quantities together, meaning if the user chose the same item with different quantities (eg. user selected pin pasta in the first iteration and quantity, on the second iteration user selected again pink pasta with quantity of 3),Since its the same item, i've to add first iteration quantity '2' with second iteration quantity '3' to be in total 5. The dictionary content would be {'item':[5,3.5]}我试图接近的是我想将所有数量加在一起,这意味着如果用户选择了具有不同数量的相同项目(例如,用户在第一次迭代和数量中选择了 pin 意大利面,在第二次迭代中用户再次选择了带有数量的粉红色意大利面3),由于是同一个项目,我必须将第一次迭代数量“2”与第二次迭代数量“3”相加,总共为5。字典内容为{'item':[5,3.5]}

In my code I will get n keys with 2 values, the dictionary defined as global..在我的代码中,我将获得带有 2 个值的 n 个键,字典定义为全局..

this is the output of my code:这是我的代码的输出:

{'Pink Pasta ': [2, 3.5, 5, 3.5], 'Mushroum Risto ': [3, 5.75, 7, 5.75]}

first iteration user chose pink pasta, quantity = 2
Second iteration user again chose pink pasta, quantity = 3
Third iteration user chose mushroom Risotto, quantity = 3
Fourth iteration again user chose mushroom Risotto, quantity = 4

this is what am seeking for:这就是我所寻求的:

{'Pink Pasta ': [5, 3.5], 'Mushroum Risto ': [7, 5.75]}

this is a part of my code,这是我代码的一部分,

choise = int(input('Enter dish number: '))
quantity = int(input('Enter how many dishes: '))

item = list(MyDictonary)[choise-1] #extracting the key
DishPrice = float(MyDictonary[item]) #extracting value

if item in Items: #checking if the item is already selected 
   Items[item] += quantity   #if yes, update the new quantity + 
   previous quantity
else:
    Items[item] = quantity

toCart.setdefault(item,[])
if item in Items:
    toCart[item].append(Items[item])
    toCart[item].append(DishPrice)
print(toCart)

You shouldn't be appending to the cart item.您不应该附加到购物车项目。 You already calculated the total, so just replace the cart item list with the list with the new quantity and the price.您已经计算了总数,因此只需将购物车项目列表替换为具有新数量和价格的列表。

choise = int(input('Enter dish number: '))
quantity = int(input('Enter how many dishes: '))

item = list(MyDictonary)[choise-1] #extracting the key
DishPrice = float(MyDictonary[item]) #extracting value

if item in Items: #checking if the item is already selected 
    Items[item] += quantity   #if yes, update the new quantity + previous quantity
else:
    Items[item] = quantity

toCart[item] = [Items[item], DishPrice]
print(toCart)

There's also no need for the second if item in Items: , since you just created it 2 lines before.也不需要if item in Items:的第二个if item in Items: ,因为您刚刚在 2 行之前创建了它。

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

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