Home > Net >  How to mention the number of occurences of each item in a list alongside the item without repeating
How to mention the number of occurences of each item in a list alongside the item without repeating

Time:10-06

I'm a total beginner and this is my first question on StackOverflow, sorry if this is a stupid question. But..The following is the doubt that I have: I succeeded where the text required me to (I am using Python Crash Course by Eric Matthes) but I failed where I tried to incorporate my idea in addition to what the textbook recommended; The book only asked to do the following: create a list of sandwiches and print the following:
I made your Beef sandwich.
I made your Beef sandwich.
I made your Fish sandwich.
I made your Egg sandwich.
I made your Beef sandwich.
I made your Ham sandwich.
I made your Egg sandwich.
I made your Chicken sandwich.

The following sandwiches were made:
Beef
Beef
Fish
.....
..... etc, etc.


Now this is what I tried to do:
INSTEAD of the items being simply listed one after the other under the "The following sandwiches were made" title, to mention in brackets the number of the same sandwiches ordered. i.e, like this:
Beef(3)
Fish(1)
Egg(2)
......etc
Now, with what I did, I got it, but with a minor issue:
This is what I get:
Beef(3)
Beef(3)
Beef(3)
Egg(2)
Egg(2).......
You might have hopefully understood the problem now, How do I avoid the repetitions???

The following is the code that I wrote:

while sandwich_orders:
    in_between = sandwich_orders.pop()
    print("I made your "   in_between.title()   " sandwich.")
    finished_sandwiches.append(in_between)
print("The following sandwiches were made: ")
temp = 1

for sandwiches in finished_sandwiches:
    while finished_sandwiches.count(sandwiches) > 1:
         temp  = 1
         finished_sandwiches.remove(sandwiches)
         continue
         if sandwiches == 1:
             print(sandwiches.title()   "("   str(temp)   ")")

I have to something to confess: The above code (which I think is close to solving the problem), doesn't cause the problem that I mentioned I'm having anymore, while I was writing this, I tried something and now in fact there's no more output under the title-"The following sandwiches were made:" (I understood the reason for the problem causing those repetitions but now I've landed on an another problem.)

CodePudding user response:

Assuming you have finished_sandwiches as a list of strings (cause you added their titles to your list), you can do so many methods to count them, here are two simple methods in the following code:

  • using collections.Counter
  • using a dictionary
from collections import Counter

finished_sandwiches = ['Beef', 'Beef', 'Fish', 'Egg', 'Beef', 'Ham', 'Egg', 'Chicken']

# using collections.Counter
result_1 = Counter(finished_sandwiches)

print(result_1)

# using a simple for with dictionary
result_2 = {}

for sandwich in finished_sandwiches:
    if sandwich not in result_2:
        result_2[sandwich] = 0
    result_2[sandwich]  = 1

print(result_2)

results:

Counter({'Beef': 3, 'Egg': 2, 'Fish': 1, 'Ham': 1, 'Chicken': 1})
{'Beef': 3, 'Fish': 1, 'Egg': 2, 'Ham': 1, 'Chicken': 1}

CodePudding user response:

You may try this one

sandwich_orders = ["I made your Beef sandwich.",
                   "I made your Beef sandwich.",
                   "I made your Fish sandwich.",
                   "I made your Egg sandwich.",
                   "I made your Beef sandwich.",
                   "I made your Ham sandwich.",
                   "I made your Egg sandwich.",
                   "I made your Chicken sandwich."]

ordered_items = {}

for order in sandwich_orders:
    item = order.split(' ')[3]
    if item in ordered_items:
        ordered_items[item]  = 1
    else:
        ordered_items.update({item: 1})

print("The following sandwiches were made:")

for item in ordered_items.items():
    print("{}({})".format(item[0], item[1]))

Which returns

The following sandwiches were made:
Beef(3)
Fish(1)
Egg(2)
Ham(1)
Chicken(1)
  • Related