-4

I have the following list:

fruits = [“apple”, “banana”, “grape”, “kiwi”, “banana”, “apple”, “apple”, “watermelon”, “kiwi”, “banana”, “apple”,]

Now I have to develop a function called count_the_fruits that would take as its arguments a list of fruits and a variable argument list called words. The function should use dictionary comprehension to create a dictionary of words (key) and their corresponding counts (value).

words = ["apple", "banana", "kiwi"]

Expect Output: {apple: 4, 'banana': 3, 'kiwi': 2}

Any help would be appreciated. Thanks!

  • 1
    Could you show us what you have tried so far? – Sayandip Dutta Feb 10 '20 at 04:10
  • ```def count_the_fruits(fruitList, word): lower_list = [i.lower() for i in fruitList] split_list = [i.split() for i in lower_list] count = [i.count(word) for i in split_list] print (sum(count))``` ```count_the_fruits(fruits, "apple")``` I have done this but this is for searching only one one item, not the list of item. – Arif Shahriar Feb 10 '20 at 04:15
  • Please put your attempt in the question itself, people here don't tend to take much liking to questions without showing any efforts. This might help you with the downvotes you got. – Sayandip Dutta Feb 10 '20 at 04:30
  • Thanks, Sayandip for your help and the piece of advice. I am really new here, but I will keep that in mind. – Arif Shahriar Feb 10 '20 at 04:42

3 Answers3

1

The reason it is giving you only one count is because you are only searching for one. Try this:

fruits = ['apple', 'banana', 'grape', 'kiwi', 'banana', 'apple',
          'apple', 'watermelon', 'kiwi', 'banana', 'apple']
words = ["apple", "banana", "kiwi"]

def count_the_fruits(fruits, words):
    # This is a dict comprehension
    counts = {word: fruits.count(word) for word in words}
    return counts

print(count_the_fruits(fruits, words))

Output:

{'apple': 4, 'banana': 3, 'kiwi': 2}
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
1

I got you man, but try to ask your questions better.

words =[]


def count_the_fruits():
    for fruit in fruits:
        if words.count(fruit) >=1:
            continue
        words.append((fruit, fruits.count(fruit)))
    print(words)


fruits = ["apple", "banana","grape", "kiwi", "banana", "apple", "apple", "watermelon", "kiwi", "banana", "apple"]
count_the_fruits()
Exosylver
  • 135
  • 1
  • 14
0

How about?

def count_the_fruits(fruits, fruits_to_check_for):

    # initialize variables:
    fruit_count = {}

    # iterate over each fruit in fruit list:
    for fruit in fruits_to_check_for:

        # count number of occurences:
        number_of_fruits = len([x for x in fruits if x==fruit])

        # add to dictionary:
        fruit_count[fruit] = number_of_fruits

    return fruit_count



if __name__ == '__main__':

    fruits = ['apple', 'banana', 'grape',       'kiwi', 
              'banana',  'apple', 'apple', 'watermelon', 
                'kiwi', 'banana', 'apple',]

    fruits_to_check_for = ['apple', 'banana']

    result = count_the_fruits(fruits, fruits_to_check_for)

    print(result)
Daniel
  • 3,228
  • 1
  • 7
  • 23
  • Thanks, Daniel. It helped me to understand the context. Just wondering why I am getting down-vote for my question? Is there anything that I can do to make my question more clear? – Arif Shahriar Feb 10 '20 at 04:34
  • No problem - you may have gotten down voted because you did not include your code in your question (even though you did in a comment). Don't worry too much about it and be sure to let us know if you need any more help. – Daniel Feb 10 '20 at 04:52