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

Asked

Viewed 326 times

0

I am doubtful in a college exercise, I need to store 3 data which are: Vehicle number, mileage and consumption in each variable of type list, but due to be different types, with the first 2 being integer and the last float, apart from having the input on the same line, I can’t store them.

I thought I’d try 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 (idcar and kms), to integer but for my misfortune appears this error:

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

Since the entries are given this way, with 10 different values:

1001 231 59.2

1002 496 60.4

...

Adding important information that I forgot to put: At the end I should calculate the average consumption (Km/L) for each identified car and finally at the exit I should print for each car its average consumption, besides telling which are the 2 worst consumptions between cars.

I would like very much that they could help me, already grateful, I hope that I have written in a clear way my doubts, it is the first time that I write here.

2 answers

2


If the values are related to each other, do not store them in different structures, this will only increase the complexity of their application. It will be easier for you to have just 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)))

This way your list will look like:

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

Where the identifier is the index 0 of the tuple, the mileage the index 1 and the consumption the index 2. To calculate the yield, it is enough to divide the mileage by the 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)

Which would generate a similar exit:

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

For vehicles with lower throughput, read about Python’s native function min and its parameter key.

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

  • I read what you suggested, it really seems like a great way to accomplish but maybe it’s because of my inexperience, I couldn’t insert the entry in the tuple and store it due to the values being inserted in the same line, subsequently the problem also asks to use the values of consumption and km to generate the average consumption (km/L) of each identified car and in the end I must print 2 lower values of the average consumption identify the number of the vehicle in which they are attached, as well as printing on each line the identification number of each car and its average consumption.

  • @Vinicius added a few more comments in the reply; I believe that with this you can finish it alone.

  • Solved the problem, was very fast after reading the functions that were very important for the exercise.

0

It is possible to pass a list inside 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]))
  • If I use this I get the error in the program: Traceback (most recent call last): &#xA; File "consumo1.py", line 16, in <module> &#xA; dado = [input(), input(), input()] &#xA; File "<string>", line 1 &#xA; 1001 231 59.2 &#xA; ^ &#xA;SyntaxError: invalid syntax

  • Could you tell me the error? I put the code on repl it. and it’s working

  • I’ll try to send a picture of what came up: link

  • Ali informs error on line 16, the code I sent has 10 lines, has how to add in the question the whole code?

  • Of course, it is practically with its code only and the small change that was to remove from the inputs that formatting, because the program that checks the inputs and output that is the VPL, has serious problems with this. link

  • even with the text within the inputs is generating this error? - Take a look at this one link

  • Yes, I just tested continues with the same problem, has great chances of being the VPL, it is not rare he has these random problems, would not spend so much headache if he was optimized for this, basically it is done facing the C language that is standard in college in the area of my course, although it has its variation to Python, but as you can see there are enough problems, if this is the case only by communicating to the team that takes care to try to release a new update in it or I will have to look for another way to make this entry in the program.

  • The error is in using the input in place of raw_input. The first attempts to parse the input as Python code, generating the syntax error; and calling the function three times would wait for the inputs on different lines, not on the same line.

  • @Vinicius, I updated the code, could test again.

  • @Andersoncarloswoss, wow. I didn’t know that. Thanks.

  • @Barbetta the question is in Python 2.7, so use raw_input instead of input; and notice that the inputs are on the same line, so there is no way to use three times the function.

  • @Andersoncarloswoss, I updated, but your answer is 1KK of times better, +1 ;)

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.