Python Forum
Count in Dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Count in Dictionaries
#1
This is the task that has to completed by using a dictionary of some sort.

You are curious about the most popular and least popular colours of cars and decide to write a program to calculate the frequency of car colours.

Your program should read in the colour of each car until a blank line is entered, and then print out (in any order) all the different colours of car with counts.

For example:

Output:
Car: red Car: white Car: blue Car: green Car: white Car: silver Car: Cars that are green: 1 Cars that are silver: 1 Cars that are red: 1 Cars that are white: 2 Cars that are blue: 1

Here is another example:


Output:
Car: red Car: white Car: white Car: red Car: white Car: white Car: white Car: Cars that are red: 2 Cars that are white: 5 ​
This is what I have done:

dictionary={}
values=[]
unique=[]
colour=input('Car: ')
values.append(colour)
dictionary['car']=colour


while colour != '':
  colour=input('Car: ')
  values.append(colour)
  dictionary[colour]=' '
print(dictionary)
values=set(values)
values=list(values)


for i in values:
  if i != "":
    unique.append(i)
print(unique)


for i in unique:
  a=((i in dictionary).count())
And this is the output including some inputs that I have provided:

Output:
Car: a Car: s Car: d Car: a Car: a Car: {'car': 'a', 's': ' ', 'd': ' ', 'a': ' ', '': ' '} ['a', 's', 'd']
Error:
Traceback (most recent call last): File "program.py", line 25, in <module> a=((i in dictionary).count()) AttributeError: 'bool' object has no attribute 'count'
I need to count the unique values and print how many times they appear. Please help.
Reply


Messages In This Thread
Count in Dictionaries - by Wolfpack2605 - Jan-28-2018, 01:15 AM
RE: Count in Dictionaries - by Mekire - Jan-28-2018, 01:58 AM
RE: Count in Dictionaries - by ka06059 - Jan-28-2018, 03:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Count Letters in a Sentence (without using the Count Function) bhill 3 5,324 Jun-19-2018, 02:52 AM
Last Post: bhill

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020