Reputation: 113
This is more of a continuation of the same python program I was using in my previous question. I have 14 lists. 10 lists contain the make and model of a car, followed by the value of each car, followed by the quantity of cars of each make/model. 3 lists contain the appended values from the first ten; one contains strictly the make/model, the next contains strictly the integer value of each car, and the last contains strictly the quantity of each make/model. The 14th list contains the first 10 lists as one main list.
I'm trying to find the highest integer value of a car, then find the corresponding string name of that car, and print both to the screen. I have the highest integer value, and have it formatted to print as currency, but I do not know how to find the name of the car that corresponds to that integer value.
I'll add examples for context.
My current code:
def getList(all):
x = 0
for x in range(0, len(all)):
list0.append(all[x][0])
list1.append(all[x][1])
list2.append(all[x][2])
print()
print("Vehicle Inventory by Type")
print("Inventory Make/Model")
for x in range(0, len(all)):
print("%9d %-1s" % (list2[x], list0[x]))
return all, list0, list1, list2
def getHighest(all, list0, list1):
print("Highest MRSP:")
i = max(list1)
if i == (all[9][1]):
print("yes")
print('${:,.2f}'.format(i))
list0 = []
list1 = []
list2 = []
car1 = ["Chevy Bolt EV", 37495, 2]
car2 = ["Kia Niro", 24485, 23]
car3 = ["VW e-Golf", 32790, 12]
car4 = ["Hyundai Kona", 37495, 3]
car5 = ["Honda Insight", 23725, 4]
car6 = ["Chevrolet Volt", 34395, 14]
car7 = ["Hyundai Ioniq", 23285, 5]
car8 = ["Tesla Model 3", 45200, 1]
car9 = ["Audi e-tron", 75795, 2]
car10 = ["Toyota Prius", 24405, 12]
all = (car1, car2, car3, car4, car5, car6, car7, car8, car9, car10)
getList(all)
print()
getHighest(all, list0, list1)
The "if" statement in the function "getHighest" is my attempt to try and figure out the logic for this, but I know I have it wrong, and I don't know how to get what I am looking for.
Forgot to add the result from the function "getList":
Vehicle Inventory by Type
Inventory Make/Model
2 Chevy Bolt EV
23 Kia Niro
12 VW e-Golf
3 Hyundai Kona
4 Honda Insight
14 Chevrolet Volt
5 Hyundai Ioniq
1 Tesla Model 3
2 Audi e-tron
12 Toyota Prius
As an example, the Audi e-tron would have the highest value in this specific series of lists, with an integer value of 75795, so I would like to grab both the value and the name and display both to the screen.
What I am looking for as an example:
Highest MSRP:
Audi e-tron at $75,795.00
What I have currently as an example:
Highest MSRP:
$75,795.00
I have attempted to research this type of problem on Google, but I can't seem to find anything relevant. Whether that is from my not explaining it correctly in my search, or my not looking in the right places, I don't know. Regardless, any help would be greatly appreciated, as I don't know what I should be doing.
Upvotes: 0
Views: 74
Reputation: 3744
you can use max
here, use the second element MRSP
x[1]
to evaluate:
all_cars = [car1, car2, car3, car4, car5, car6, car7, car8, car9, car10]
max_car = max(all_cars , key=lambda x:x[1])
print('{} at ${:,.2f}'.format(*max_car))
output:
Audi e-tron at $75,795.00
Upvotes: 1
Reputation: 1493
Change your function def getHighest()
with this one:
def getHighest(all, list0, list1):
print("Highest MRSP:")
i = max(list1)
c = [j for j, lst in enumerate(all) if i in lst][0]
car = all[c][0]
if i == (all[9][1]):
print("yes")
print('{} at ${:,.2f}'.format(car,i))
Upvotes: 2
Reputation: 8521
You can use a Python library called pandas
, which will make your task much more easier.
Can you try the following:
car1 = ["Chevy Bolt EV", 37495, 2]
car2 = ["Kia Niro", 24485, 23]
car3 = ["VW e-Golf", 32790, 12]
car4 = ["Hyundai Kona", 37495, 3]
car5 = ["Honda Insight", 23725, 4]
car6 = ["Chevrolet Volt", 34395, 14]
car7 = ["Hyundai Ioniq", 23285, 5]
car8 = ["Tesla Model 3", 45200, 1]
car9 = ["Audi e-tron", 75795, 2]
car10 = ["Toyota Prius", 24405, 12]
all_cars = [car1, car2, car3, car4, car5, car6, car7, car8, car9, car10]
import pandas as pd
df = pd.DataFrame(all_cars, columns=['car_name', 'price', 'qty'])
print(df.sort_values(by=['price'], ascending=[False]))
If you want to get the first values, you can do the following:
result = df.sort_values(by=['price'], ascending=[False]).iloc[0]
print(result['car_name'])
print("{:,}".format(result['price']))
Upvotes: 2