-4

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!

  • 2
    `number of rows in csv file`, is your code running ? There is so many syntax errors in your code. – scharette Nov 27 '17 at 15:02
  • 1
    You are missing (at least) some colons... – mrCarnivore Nov 27 '17 at 15:03
  • I am sorry, this is pseudocode, I will update it. – user9015060 Nov 27 '17 at 15:06
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) ... maybe keep your dictionaries in a dictionary, then you can construct strings to match the keys. – wwii Nov 27 '17 at 15:06

1 Answers1

0

You don't want to have separate variables for basket1, basket2, and so on. You want to have a list of dictionaries.

baskets = [ {'total':0, 'max':5, 'apple':0, 'banana':0, 'melon':0, 'orange':0,'other':0},
            {'total':0, 'max':5, 'apple':0, 'banana':0, 'melon':0, 'orange':0,'other':0},
            ... ]

Then to access the j-th basket, simply call baskets[j]. Note that the more idiomatic way to write the loop is not for j in range(len(baskets)) but rather for basket in baskets.

This is a rather common issue to run into when first starting to code, in my experience. If you find yourself wanting to label your variables with 1, 2, etc and you want to call them depending on their label, you really need an array. It goes right along with the oddly common strategy of trying to keep several lists at once that are parallel with each other when you really just need to use a class.

colopop
  • 441
  • 3
  • 13