#Python Help - Create an inventory based on a list

46 messages Β· Page 1 of 1 (latest)

latent vault
#

Hello c:

I'm trying to create a diction that counts the amount of times an item reoccurs in a list and put the result in a diction with the respective item.

here is my code:
def create_inventory(items):
"""Create a dict that tracks the amount (count) of each element on the items list.

:param items: list - list of items to create an inventory from.
:return: dict - the inventory dictionary.
"""

di = dict()
for i in items:
    di = {i:items.count(i)}
return di

here is my error message:
AssertionError: {'diamond': 2} != {'wood': 1, 'iron': 2, 'diamond': 2}

  • {'diamond': 2}
  • {'diamond': 2, 'iron': 2, 'wood': 1} : Called create_inventory(["wood", "iron", "iron", "diamond", "diamond"]). The function returned {'diamond': 2}, but the tests expected {'wood': 1, 'iron': 2, 'diamond': 2}.

it looks like it is only making one entry in the diction for the last item. I can't figure out how to save each item and count value.

zenith stone
#

What does this line do?

        di = {i:items.count(i)}
latent vault
#

It creates the diction with i (item) as the key and the count of i as the associated value (I think?)

zenith stone
#

Correct. This creates a new dictionary.

latent vault
#

ohhhhhh

zenith stone
#

Which means the dictionary with the prior item is ... gone.

latent vault
#

I see! let me see if I can get a solution now

zenith stone
#

Good luck πŸ™‚ Let us know if you solve it. Or if you're still stuck.

#

And, if you haven't yet done so, make sure you read/skim the built-in dict methods πŸ™‚ They are handy for this exercise.

latent vault
#

So, I understand what I was doing wrong but I am having a hard time thinking of a solution. I'm thinking I need to create a new list with the count values and then combine it with the items list? does that sound like the right direction?

zenith stone
latent vault
#

I would write the first word down, count the word reoccurrences in the list, write the value next to the word and continue on down the list

zenith stone
#

That works, but isn't very efficient ... nor how inventories usually work. What if you didn't have the whole list but instead were getting one item read to you at a time from a list?

latent vault
#

I would count how many times each word is read out loud and only keep the final value for each word

zenith stone
#

That's how I'd do it, too!

#

Could you break that down into simple steps?

latent vault
#

so as a word is given to me, I would add 1 to that value and forget about the previous value. I would also need to keep track of the words just in case there are so many so I'd have a list of unique words and a value that adds 1 to it every time it is read out loud

#

hopefully that is what you meant by simple steps

zenith stone
#

Let's assume you got a paper and pen and I'm reading items to you. I read, "Apple". What do you do?

latent vault
#

I would write apple

zenith stone
#

Okay. Then I read "orange". What do you do?

latent vault
#

I would write orange underneath apple

zenith stone
#

I read "Apple". What do you do?

latent vault
#

I would create a tally next to apple adding 1 everytime I hear you say apple

zenith stone
#

That sounds like an algorithm and not a specific action πŸ˜‰ Are you changing your prior answer?

latent vault
#

should I write apple underneath orange? so I'd create a full list of all the items you read off?

latent vault
zenith stone
#

Yeah. That's just creating the list, not a tally

zenith stone
latent vault
#

check if the word you read has already been read or if it is new. If it is new, add the word under orange. if it is not new, I would add a hash mark to my tally to respective word

zenith stone
#

That sounds like a good approach. Could you translate those exact steps into code?

latent vault
#

I think I can. let me give it a try and I'll get back to this thread!

zenith stone
#

Good luck πŸ™‚

latent vault
#

this worked!

latent vault
zenith stone
#

Hah! That's not the described steps from above

#

It solves the task but it's not the optimal solution and it doesn't actually implement the algorithm you described

latent vault
#

let me try and solve it from the steps above c:

zenith stone
#

Going from steps to code that actually does the steps is a pretty valuable skill

latent vault
#

Is there better?

#

took me way longer than I am proud to admit lol

zenith stone
#

Use codeblocks to share code πŸ™‚ Images are much harder to deal with than text!!

#

Yes, this is a whole lot closer to the steps you described above