1

I would like to create a list in Python with n elements that have the same distance between each other. So the seried list should looke like [0.05, 0.1, 0.15 ..., 0.95, 1]. I tried the following but it did not work:

list = [range(0.05, 1, 0.05)]
list_2 = [i for i in range(0.05, 1)]

Any idea, how I can do that?

PeterBe
  • 700
  • 1
  • 17
  • 37
  • `whatever = [t/100.0 for t in range(5, 100, 5)]` ? -> beware of Floating math broken – Patrick Artner Mar 02 '22 at 11:19
  • This is somewhat tricky because of round-off error. A naive while loop containing `x += 0.05` will terminate with `0.9500000000000003` (not enough space for one more `.05` in the target range). – John Coleman Mar 02 '22 at 11:23
  • @PatrickArtner: Thanks for your answer. But what if I want to have a larger list with elements from [0.025, 0.05, 0.075, 0.1, ... , 1] and I tried your suggested appraoch `whatever = [t/100.0 for t in range(0.025, 1025, 2.5)]` but I get an error "TypeError: 'float' object cannot be interpreted as an integer". Is there not a direct way of doing this like in R? – PeterBe Mar 02 '22 at 11:30
  • Why do you feed `range()` with float values - that won't fly - `range(...)` only works with INTegers. – Patrick Artner Mar 02 '22 at 11:45
  • 1
    @PeterBe did you read the DUPLICATE? did you understand its answers? numpy has a float-able range command. The way around importign the huge numpy package is to calculate your numbers yourself: If you want 1000 values between 0+offset and 1+offset (exclusive) you would do `offset = 1 / 1000.0; ttt =[value/1000.0+offfset for value in range(1000)]` etc. The reason it does not work as it does in R is that this is python ... not R. – Patrick Artner Mar 02 '22 at 11:48
  • [typesseq-range](https://docs.python.org/3/library/stdtypes.html#typesseq-range) : ==> **The arguments to the range constructor must be integers** – Patrick Artner Mar 02 '22 at 11:51
  • @PatrickArtner: Thanks for your answer. I use your approach with the offset. – PeterBe Mar 02 '22 at 12:03

0 Answers0