如何遍历3个列表并在Python中获得3列?

时间:2016-01-31 05:54:26

标签: python

我有三个列表list1是" Car Models" list2是"每加仑的价格(mpg)"和list3是"一年的总价格"。我能够遍历3列,但我不知道如何一个接一个地获得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

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

因为您想要的每个列具有相同数量的项目,所以您只需要使用一个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]

这应该有效

答案 2 :(得分:1)

我认为以下解决方案更具有pythonic

data UnnecessaryAlgebraicDataType = ResponseType1 | ResponseType1 | ResponseType3
rpcId2HaskellType :: String -> UnnecessaryAlgebraicDataType

答案 3 :(得分: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))))

输出:我正在运行Python3.4

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

答案 4 :(得分:1)

您可以使用pandas处理类似表格(又称DataFrame)结构中的数据。

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数据结构中有很多方便的选项。

答案 5 :(得分: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]

根据您的需要安排输出。