2016-01-31 87 views
0

我有三個清單list1是「汽車模型」list2是「每加侖價格(mpg)」,list3是「一年的總價」。我能夠遍歷3列,但我不知道如何依次獲得3列。請有人幫助我。先謝謝你!!如何遍歷3個列表並獲取Python中的3列?

繼承人我的代碼:

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) 

我想輸出是這樣的:

MODEL COST(MPG) TOTALCOST 
Toyota 15.0  1606.66 
Nissan 20.0  1205.0 
Honda 30.0  803.33 

回答

2

試試這個:

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]) 

更多更好的格式,使用format

+0

但是,這並不在Python 3.4 – HenryDev

+0

@Henry工作,將我更新的代碼 – Hackaholic

+0

我得到這個錯誤信息,你更新之後: – HenryDev

1

我想下面的解決辦法是更Python

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 
1

既然你是在談論循環的同時,我會強烈建議使用zip。看下面的例子!

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:我正在運行Python3.4

Model Cost(MPG) TOTALCOST 
Toyota 15.0 1606.6666666666667 
Nissan 20.0 1205.0 
Honda 30.0 803.3333333333334 
+0

但我使用python 3.4和你的代碼不工作..任何想法爲什麼? – HenryDev

+0

你現在可以試試嗎?我更新了我的代碼 – python

1

可以使用大熊貓在一個表樣(又名數據幀)結構數據的工作。

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) 

這樣做的好處是,如果你最終想要做更高級的數據操縱或格式化,也有很多在pandas.DataFrame數據結構方便的選項。

1

你可以試試這個。

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] 

根據您的需要排列輸出。

1

因爲你想要的每一列都有相同數量的項目,所以你只需要使用一個for循環。您還需要爲此創建一個totalCost數組。

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] 

這應該工作