Reputation: 4983
I have three lists list1 is "Car Models" list2 is "price per galon (mpg)" and list3 is "total price for one year". I'm able to loop through the 3 columns, but I don't know how to get 3 columns one after the other. Please someone help me. Thank you in advance!!
Heres my code:
modelName = ['Toyota', 'Nissan', 'Honda']
fuelEfficiency = [15.0, 20.0, 30.0]
for i in range(len(modelName)):
print (modelName[i])
for i in range(len(fuelEfficiency )):
print (fuelEfficiency[i])
priceGas = 2.41
totalCost1 = (10000 * priceGas)/15.0
totalCost2 = (10000 * priceGas)/20.0
totalCost3 = (10000 * priceGas)/30.0
print (totalCost1)
print (totalCost2)
print (totalCost3)
The output I want is this:
MODEL COST(MPG) TOTALCOST
Toyota 15.0 1606.66
Nissan 20.0 1205.0
Honda 30.0 803.33
Upvotes: 0
Views: 3160
Reputation: 86
Because every column that you want has the same amount of items, you only need to use one for loop. You also need to make a totalCost array for this to work.
modelName = ['Toyota', 'Nissan', 'Honda']
fuelEfficiency = [15.0, 20.0, 30.0]
priceGas = 2.41
totalCost = [0, 0, 0] # Filler values
totalCost[0] = (10000 * priceGas)/15.0
totalCost[1] = (10000 * priceGas)/20.0
totalCost[2] = (10000 * priceGas)/30.0
for i in range(len(modelName)):
print modelName[i], fuelEfficiency[i], totalCost[i]
This should work
Upvotes: 2
Reputation: 19753
Try this:
modelName = ['Toyota', 'Nissan', 'Honda']
fuelEfficiency = [15.0, 20.0, 30.0]
priceGas = 2.41
totalCost1 = (10000 * priceGas)/15.0
totalCost2 = (10000 * priceGas)/20.0 # totalCost = list(map(lambda x: (10000*priceGas)/x, fuelEfficiency))
totalCost3 = (10000 * priceGas)/30.0
totalCost = [totalCost1, totalCost2, totalCost3]
print("Model", "Cost(MPG)", "TOTALCOST")
for x in range(3):
print(modelName[x], fuelEfficiency[x], totalCost[x])
for more better format, use format
Upvotes: 2
Reputation: 4521
Since you are talking about looping parallel, I would highly recommend to use zip
. See the below example!
modelName = ['Toyota', 'Nissan', 'Honda']
fuelEfficiency = [15.0, 20.0, 30.0]
priceGas = 2.41
totalCost1 = (10000 * priceGas)/15.0
totalCost2 = (10000 * priceGas)/20.0
totalCost3 = (10000 * priceGas)/30.0
totalcost = [totalCost1, totalCost2, totalCost3]
print ("Model", "Cost(MPG)", "TOTALCOST")
for i in zip(modelName, fuelEfficiency, totalcost):
print (" ".join(map(str, list(i))))
OUTPUT : I am running Python3.4
Model Cost(MPG) TOTALCOST
Toyota 15.0 1606.6666666666667
Nissan 20.0 1205.0
Honda 30.0 803.3333333333334
Upvotes: 1
Reputation: 368
You can Try this.
modelName = ['Toyota', 'Nissan', 'Honda']
fuelEfficiency = [15.0, 20.0, 30.0]
for i in range(len(modelName)):
print (modelName[i])
for i in range(len(fuelEfficiency )):
print (fuelEfficiency[i])
priceGas = 2.41
totalCost=[0]*len(fuelEfficiency)
for i in range(len(fuelEfficiency)):
totalCost[i]=(10000 * priceGas)/fuelEfficiency[i]
print "Model Cost Totalcost"
for i in range(len(fuelEfficiency)):
print modelName[i],fuelEfficiency[i],totalCost[i]
Arrange the output according to your need.
Upvotes: 1
Reputation: 1903
You can use pandas for working with data in a table-like (aka DataFrame) structure.
import pandas as pd
modelName = ['Toyota', 'Nissan', 'Honda']
fuelEfficiency = [15.0, 20.0, 30.0]
priceGas = 2.41
df = pd.DataFrame({'MODEL': modelName, 'COST(MPG)': fuelEfficiency})
df['TOTALCOST'] = (10000 * priceGas) / df['COST(MPG)']
print df[['MODEL', 'COST(MPG)', 'TOTALCOST']].to_string(index=False)
This has the advantage that if you end up wanting to do more advanced data manipulation or formatting, there are a lot of convenient options in the pandas.DataFrame data structure.
Upvotes: 1
Reputation: 11144
I think below solution is more pythonic
modelName = ['Toyota', 'Nissan', 'Honda']
fuelEfficiency = [15.0, 20.0, 30.0]
priceGas = 2.41
totalcost = map(lambda x:(10000 * priceGas)/x,fuelEfficiency)
for a,b,c in zip(modelName,fuelEfficiency,totalcost ):
print a,b ,c
Upvotes: 1