Limit the amount of decimals of a floats values that fill a list

Asked

Viewed 274 times

0

Hello Stackoverflow community, good morning.

I’m trying to develop a code that calculates the numerical values obtained from a physics equation

Here is the form of the equation, t

where k , T and pi are user-defined constant values. Already lambda is a value that will be traversed within a range with for and its responses stored in a list using the following code snippet

import pandas as pd
import numpy as np


k=2.0
T=25.0+273.0
rhos=[]
for i in range (1,100):

rho = (8*np.pi*k)/i
rhos.append(rho)
print (rhos)

the results are coming out in this format :

inserir a descrição da imagem aqui

How do I control how many decimals will appear on the list? This control can be done before filling in the list or only after filling in the append method?


EDIT 1 : I tried to adjust the code as follows,

 import pandas as pd
import numpy as np


k=2.0
T=25.0+273.0

rhos=[]
for i in range (1,100):

rho = (8*np.pi*k)/i
rhos.append(rho)
print( ".4f" % rho )

However I am getting this error message, apparently I converted the strings to text when filling the list.

inserir a descrição da imagem aqui

I will give a study on the chapter formatting with % and try to adjust the code,

until then, thanks to Marcelo for helping the/

2 answers

1

First of all, the values saved in the Rhos list will be floating point variables, not strings(texts), so it is not necessary to limit the number of commas, doing this would still cause loss in the accuracy of your calculations.

At the moment of presenting this data to the user is that we must limit the commas, for such, one must use the format of strings in python. See more in Python Docs.

The standard use is:

variavel = 3.141516
print( "%.2f" %variavel)
# 3.14

Where inside your print, Voce puts in quotation marks %.[decimal places][type]. Above I put . 2 to indicate two decimal places, f to indicate that it is a floating point type number (rational number).

In your case, Voce has a list of values. You are printing the entire list several times within your FOR. I don’t know if that’s your intention. To print the list only once remove the current print and insert after Rhos.append(Rho) the following code:

    print( "%.4f" % rho )

This will print your list, line by line, to 4 decimal places,

  • Unfortunately I couldn’t :/ The idea was to display the list of values only once after the list had been filled in with the values of the equation. I tried to do as you suggested but I think I did something wrong, follow a print of the result and code

  • 1

    Sorry, I forgot to put the % in the example, the correct print would be print( "%.4f" %Rho ). In the rest your code will work

  • Oops, I just saw your reply. I later edit with the result. :)

  • Buddy, it didn’t work. I don’t understand why :(

1

To display the list values with a number of specific decimal places you can use the function round(), specifying the number of decimals. In this case the code would look like this:

import pandas as pd
import numpy as np


k = 2.0
T = 25.0 + 273.0
rhos = []
for i in range(1, 100):
    rho = round((8 * np.pi * k) / i, 4)
    rhos.append(rho)
print(rhos)

Or in this way:

import pandas as pd
import numpy as np


k = 2.0
T = 25.0 + 273.0
rhos = []
for i in range(1, 100): 
    rhos.append(round((8 * np.pi * k) / i, 4))
print(rhos)

Or, using List Comprehensions.

import pandas as pd
import numpy as np


k = 2.0
T = 25.0 + 273.0
rhos = [round((8 * np.pi * k) / i, 4) for i in range(1, 100)]

print(rhos)

OBSERVATION 1: In the code you gave us, you imported the unused pandas library, though. This can cause some kind of error in some IDE’s. It is only right to import a library if, in fact, you use it.

OBSERVATION 2: I noticed you set the variable T however, you did not use it either. Only define a variable if you use it.

Browser other questions tagged

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