I have given csv file which I divided into rows with semicolons. Each row has a place, product name and amount. It was pretty easy to add each product to list if the amount was 1. But when the amount is >1, then comes the problem.
Dictionary is for counting how many times the item is given in the list. For example:
I have a row where: Test; Chicken bowl; 4;
name = Test
, item = chicken bowl
, amount = 4
if name =='Test':
if amount == 1:
count_test += 1
test_items.append(item)
else:
count_test += 1
test_items.append(int(amount) * item)
#listt = [item] * int(amount)
#test_items.append(listt)
print(f'Test checked and count {count_test}')
print(f' {item}; {amount}; ')
items_dict = {x:test_items.count(x) for x in test_items}
print(f'Test: {items_dict}')
The desired result should be:
['FIT salmon bowl 375 kcal', 'Sushi Rice', 'Extra avocado', 'Chicken bowl',
'Sushi Rice', 'Extra coriander', 'Extra avocado', 'Chicken bowl',
'Chicken bowl','Chicken bowl','Chicken bowl','Chicken bowl', 'Sushi Rice',
'Salmon bowl','Salmon bowl' 'Sushi Rice', 'Salmon bowl',
'Salmon bowl', 'Quinoa', 'FIT salmon bowl 375 kcal', 'Quinoa']
- To dictionary to get the count of each key:
Test: {'FIT salmon bowl 375 kcal': 2,
'Sushi Rice': 4, 'Extra avocado': 2, 'Extra coriander': 1,
'Chicken bowl': 5, 'Salmon bowl': 4, 'Quinoa': 2}
But not like this which I have at the moment:
['FIT salmon bowl 375 kcal', 'Sushi Rice', 'Extra avocado', 'Chicken bowl',
'Sushi Rice', 'Extra coriander', 'Extra avocado',
'Chicken bowlChicken bowlChicken bowlChicken bowl', 'Sushi Rice',
'Salmon bowlSalmon bowl', 'Sushi Rice', 'Salmon bowlSalmon bowl',
'Quinoa', 'FIT salmon bowl 375 kcal', 'Quinoa']
Test: {'FIT salmon bowl 375 kcal': 2, 'Sushi Rice': 4, 'Extra avocado': 2,
'Chicken bowl': 1, 'Extra coriander': 1,
'Chicken bowlChicken bowlChicken bowlChicken bowl': 1,
'Salmon bowlSalmon bowl': 2, 'Quinoa': 2}