Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a list of floats with only a specific decimal [0.0, 0.7, 1.0, 1.7, 2.0, 2.7, 3.0, 3.7, 4.0, 4.7, …etc]

I am reading from an excel file and require the generation of a list to pull from where it has every integer in a range to the number read as well as a specific decimal between each integer.

For instance, if I read that I require 2.3 units of x, then I would like to generate a list in range 0 to 2.3 that has [0, 0.3, 1.0, 1.3, 2.0, 2.3].

I have thought about adding two lists together:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Units = 3.3
ListIntegers = list(range(math.ceil(Units)))
ListFloats = list(range())      # Here I would need to create the 0.3, 1.3, 2.3, and 3.3
FinalList = ListIntegers + ListFloats
 
FinalList.sort()      # This will put my list back in numerical order
  1. How would I create that list of specific float values?
  2. Is there a better way to do this?

>Solution :

How’s this?:

import math

Units = 5.8
addition = float("0." + str(Units).split(".")[1])
intList = list(range(math.ceil(Units)))
finalList = intList + [x+addition for x in intList]
finalList.sort()
print(finalList)

Output:

[0, 0.8, 1, 1.8, 2, 2.8, 3, 3.8, 4, 4.8, 5, 5.8]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading