I need to make an entry in Python storing as a list, all in one line

0

I'm doubtful in a college exercise, I need to store 3 data which are: Vehicle number, kilometers rotated and consumption in each variable of type list, but due to being different types, with the first 2 being integers and the last float, besides having the entry on the same line, I can not store them.

I thought about trying to store it this way:

idcar=[] #Numero de cada veiculo
kms=[]   #Quilometros rodados
consumo=[] #Consumo de cada veiculo
for i in range(10):
    idcar[i],kms[i],consumo[i]=map(float,raw_input().split())

And then transform the float values (the idcar and kms), to integer but to my misfortune this error appears:

Traceback (most recent call last):                                              
  File "consumo1.py", line 17, in <module>                                      
    idcar[i],kms[i],consume[i]=map(float,raw_input().split())                   
IndexError: list assignment index out of range

Given that the entries are given in this way, with 10 different values:

1001 231 59.2

1002 496 60.4

...

Adding important information that I forgot to put:   In the end I must calculate the average consumption (Km / L) for each identified car and finally in the output I should print for each car its average consumption, in addition to saying which are the 2 worst consumptions between the cars.

I would love it if you could help me, thank you in advance, I hope that I have written clearly my doubts, this is the first time I write here.

    
asked by anonymous 24.05.2018 / 03:19

2 answers

2

If values are interrelated, do not store them in separate structures, this will only increase the complexity of your application. It will be easier for you to just have a list and store all the values in it, in a tuple. For example:

veiculos = []
for i in range(10):
    identificador, quilometragem, consumo = raw_input().split()
    veiculos.append((int(identificador), int(quilometragem), float(consumo)))

In this way your list will look like:

veiculos = [
    (1001, 231, 59.2),
    (1002, 496, 60.4)
]

Where the identifier is index 0 of the tuple, the mileage the index 1 and the consumption the index 2. To calculate the yield, simply divide the mileage by consumption. You can define a function for this:

def calcular_rendimento(veiculo):
    return veiculo[1] / veiculo[2]

And thus, display the performance of vehicles:

for veiculo in veiculos:
    rendimento = calcular_rendimento(veiculo)
    print 'Veículo {} teve um rendimento de {:.2f} km/L'.format(veiculo[0], rendimento)

What would generate output similar to:

Veículo 1001 teve um rendimento de 3.90 km/L
Veículo 1002 teve um rendimento de 8.21 km/L

For less efficient vehicles, read about the native Python function min and its key parameter.

But for a more readable and idiomatic solution, I suggest you read this other answer .

    
24.05.2018 / 03:33
0

You can move a list into a list.

dados = []

for i in range(2):
    codigo, KM, consumo = raw_input().split()
    dado = [codigo, KM, consumo]
    dados.append(dado)

for item in dados:
    print("Código: {}".format(item[0]))
    print("KM: {}".format(item[1]))
    print("Concumo: {}".format(item[2]))
    
24.05.2018 / 03:32