I am trying to write a code to distribute materials to different groups. To do this I want to use dictionaries. But I need to match conditions using if and for loops and I suspect that the problem is the output of these conditionals are strings so they do not refer back to dictionaries (wrong type). I do not know in advance which dictionary to call so I cannot write them down specifically either.
Let me elaborate my problem further. My situation can be simplified as this: I own a fruit shop and I want to make fruit baskets. I want to put as different fruits as possible in each basket. I have a new supply of fruits today but baskets have some fruit in them already from yesterday.
First thing I did was to find out how many fruits each basket already has from yesterday. The data I have (as csv file) is something like this.
Fruit Basket
'apple' '1'
'apple' '2'
'apple' '4'
'orange' '4'
'melon' '1'
...
So I created empty dictionaries.
basket1 = {'total':0, 'max':5, 'apple':0, 'banana':0, 'melon':0, 'orange':0,'other':0}
basket2 = {'total':0, 'max':5, 'apple':0, 'banana':0, 'melon':0, 'orange':0,'other':0}
...
So my pseudo-code:
for i in range (number of rows in csv file):
temp = fruit in i-th row # this is a string which I hope to use as key
for j in range(number of baskets):
if basket of the i-th fruit == basket j
assignedBasket = 'basket'+'j' # this is to call the right dictionary
assignedBasket[temp] += 1 # add 1 to the correct key
I think the problem is that assignedBasket
is a string, not dictionary. How can I access the correct dictionary? Any help will be appreciated. Thank you!