Reputation: 35
My goal is to find the five most expensive laptops from a CSV file with details about the laptops and print the manufacturer, model name, and price of each.
In the screenshot, you can see the csv file
I was able to find the most expensive, however, I am having trouble finding the top 5.
import csv
class Laptops():
def __init__(self, manufacturer, model_name, category, screen_size, screen, cpu, ram,storage,gpu,os,weight, price, os_version=None):
self.manufacturer=manufacturer
self.model_name=model_name
self.category=category
self.screen_size=screen_size
self.screen=screen
self.cpu=cpu
self.ram=ram
self.storage=storage
self.gpu=gpu
self.os=os
self.weight=weight
self.price=price
self.os_version=os_version
def laptop():
with open("PATH TO FILE,) as f:
reader=csv.reader(f)
for i, row in enumerate(reader):
if i==0:
continue
else:
yield(Laptops(
row[0],
row[1],
row[2],
float(row[3][:-1]),
row[4],
row[5],
row[6],
row[7],
row[8],
row[9],
row[11],
float(row[12].replace(",",".")),
row[10]
))
def five_expensive():
most_expensive=None
object_of_max_price=None
for char in laptop():
if most_expensive:
if char.price>most_expensive:
most_expensive=char.price
object_of_max_price=char
else:
most_expensive=char.price
return f"The most expensive notebook is {object_of_max_price.manufacturer} {object_of_max_price.model_name} with price of {object_of_max_price.price} Euros "
Upvotes: 0
Views: 493
Reputation: 44148
More efficient than sorting would be to use a heap queue:
import heapq:
for laptop in heapq.nlargest(5, laptop(), lambda x: x.price):
print(laptop)
Or if you just want a list:
laptops = heapq.nlargest(5, laptop(), lambda x: x.price)
Note: If you want the N largest where N is a large number, then you would probably be better of sorting. If N is 1, then you could optimize this with just using the max
function.
Upvotes: 0
Reputation: 104
I would recommend sorting the laptops by price and then returning the first 5. So something along the lines of:
laptops = laptop()
laptops.sort(key=lambda laptop: laptop.price, reverse=True)
return laptops[:5]
Upvotes: 3