list_in = [['Игрушки', 'Мяч надувной', '300', '1'], ['Игрушки', 'Бионикл', '8000', '1'], ['Ткань', 'Вельвет', '1000', '2'], ['Ткань', 'Джинса', '500', '2'], ['Игрушки', 'Бионикл', '1000', '2']]
list_out = []
pos = {}
pos_counter = 0
for el in list_in:
if (el[0], el[3]) not in pos.keys():
pos[(el[0], el[3])] = pos_counter
pos_counter += 1
list_out.append([el[0],int(el[2]),el[3]])
else:
index = pos[(el[0], el[3])]
list_out[index][1] += int(el[2])
print(list_out)
class Product:
category: str
product_name: str
price: int
warehouse_id: int
def __init__(self, category: str, product_name: str, price: int, warehouse_id: int):
self.category = category
self.product_name = product_name
self.price = price
self.warehouse_id = warehouse_id
@property
def id(self):
return (self.category, self.warehouse_id)
products = [
Product('Игрушки', 'Мяч надувной', 300, 1),
Product('Игрушки', 'Бионикл', 8000, 1),
Product('Ткань', 'Вельвет', 1000, 2),
Product('Ткань', 'Джинса', 500, 2),
Product('Игрушки', 'Бионикл', 1000, 2)
]
result = []
product_ids = {}
product_counter = 0
for product in products:
if product.id not in product_ids.keys():
product_ids[product.id] = product_counter
product_counter += 1
result.append([product.category, product.price, product.warehouse_id])
else:
index = product_ids[product.id]
result[index][1] += product.price
print(result)
pip install vk_api
Или
pip3 install vk_api --user