Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.


0 votes

My partner and I are a little lost on how to start. We can't figure out how to print out the number of complaints associated with the company name. Any suggestions?

asked in CSC201 Spring 2021 by (701 points)

2 Answers

+2 votes

I'd recommend using a for loop to pull the company name from each complaint, append it to a list, and then use the .count() method for each company in the list.

answered by (5.5k points)

Yea I think we realized we needed a loop but we are not sure how to pull the company name from each complaint. I don't get the part where the directions say, "the list of complaints where each complaint is a dictionary."
not sure how to go about it.


So in the list of complaints, each complaint is a dictionary. That means listOfComplaints[0] is a dictionary object. So if you set complaint = listOfComplaints[0], you can now more easily access that dictionary. If you set company = complaint['Company'] that should give you the company name. If you look at the csv file, you'll notice each row is a dictionary, with each column's label being a key.


Ok, I think I get it. So those values on the very top of the text file are the keys? I am still lost on how all the rows are considered dictionaries if some don't provide a date and some other values though.


I think that is just considered "missing data." Dr. Stonedahl mentions that on the assignment sheet. It's still a dictionary, but with those keys having no value. I'm not 100% sure whether the value is None or the empty string or what, but fortunately, it doesn't break the whole dictionary.


Ah ok, thanks I was confused about that it looked like it was a continuation from another dict. Thanks.

+2 votes

First of all, you have to create a variable to count the number of complaints.

The approach is to go through each Dict of the List by using the for loop. Each Dict has the <key> labeled as "Company", while the <value> of that <key> is the actual name of the company.

Like in the PDF:

complaintDict: = {...
                  'Company': '<name of the company>'

Therefore, during the loop, you want to check if the company name the user inputted matches with the <value> of the 'Company' key of each Dict. It will be easier if you set another variable to store that <value> in order to compare it with the user's input.

Every time we go through each Dict and there is a match, then the count variable will add 1 to itself.

In the end, if that variable equal to 0, the company is definitely not on that list. Otherwise, you can print it.

answered by (2.8k points)

Would I have to make the for loop go through each dictionary then in order to find the company name for each complaint?


Yes, that's the only loop that you need, which is to go through each Dict inside the List