Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help
#1
Hey everyone,

I'm struggling with something and I was wondering if someone could give me some advice.
So I have something like this:

Output:
[{"id":1,"car":{"car_make":"Ford","car_model":"Club Wagon","car_year":1997},"price":"$5179.39","total_sales":446}, {"id":2,"car":{"car_make":"Acura","car_model":"TL","car_year":2005},"price":"$14558.19","total_sales":589}, {"id":3,"car":{"car_make":"Volkswagen","car_model":"Jetta","car_year":2006},"price":"$14879.11","total_sales":825}, {"id":4,"car":{"car_make":"Chevrolet","car_model":"Uplander","car_year":2006},"price":"$17045.06","total_sales":689}, {"id":5,"car":{"car_make":"Plymouth","car_model":"Roadrunner","car_year":1997},"price":"$14770.44","total_sales":691}]
So I have 2 questions.
1 - I need to calculate the car model that has the most sales. I have already written a code that calculates the most sales, but I can't figure out how to calculate the car_model for the most sales.
2 - For each car_year I need to calculate the total cars sold and then figure out the most popular year.

I'm new to this and have been struggling for days and just can't figure it out. Any help or advice would be very much appreciated.
Thanks a lot!
Reply
#2
(Mar-24-2020, 10:42 AM)just_me Wrote: I have already written a code that calculates the most sales,
For start you may show us what you have so far.
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi buran, thanks for your reply. This is what I have so far.

seq = [x['total_sales'] for x in cars]
most_sales = max(seq)
print(most_sales)
This gives me the most sales of all the total_sales of cars. From here on I am stuck and can't find a way to go further.
Reply
#4
One way is to understand structure of the dictionary. Maybe this will help:

>>> d = {"id":1,"car":{"car_make":"Ford","car_model":"Club Wagon","car_year":1997},"price":"$5179.39","total_sales":446}
>>> d.keys()
dict_keys(['id', 'car', 'price', 'total_sales'])
>>> d['car'].keys()
dict_keys(['car_make', 'car_model', 'car_year'])
>>> d['car']['car_make'], d['total_sales']
('Ford', 446)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Thanks perfringo, that helps a lot. But what I still can't understand is how can I go about add up the total sales for every car_year that is the same. ( like total sales for 1997, etc)?
Reply
#6
What have you thought about? How would you do it on paper?
Reply
#7
(Mar-25-2020, 02:43 PM)ndc85430 Wrote: What have you thought about? How would you do it on paper?

I would do something like this
sales_sum = sum(d['total_sales'] for d in cars if d)
print(sales_sum)
this would give me the number of total sales of all the years together. I don't know how I could do the same operation only considering each year at a time Confused
Reply
#8
Ok, so think a bit more and break the problem down further. What small step would help you be able to work it out for each year? I want verbal steps, not code, at this point.
Reply
#9
I would need to find a way to "recognize" which of the car_years in the dictionaries are the same. And at that point, for the ones that are the same, I could add them up.
Reply
#10
Ok, so how can you break that problem down further? How can you keep track of the unique years?
Reply


Forum Jump:

User Panel Messages

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