HG.KIM
HG.KIM

Reputation: 13

How to insert 1 interval numbers between given irregular numbers in python

I've generated index number list what I do find data using for-loop.

That numbers are as follows.

I want to generate 1 interval index numbers between each given numbers

Int64Index([ 2343,  2913,  5466,  8589, 10151, 11713, 13275, 14837, 15618,7961, 19523, 21624, 21700],dtype='int64')

for e.g) 2343,2344,2345,2346 ......,2911,2912,2913,2914....5464,5465,5466 some thing like that.

Is there any method that i generate 1 interval numbers do not use for-loop?

Upvotes: 0

Views: 202

Answers (2)

Errol
Errol

Reputation: 610

Try using the builtin range() function.

First you need to determine start and end number using the min() and max() functions.

start_num = min(listr)
end_num = max(listr)

Then we use range() to create our new list

new_listr = list(range(start_num, end_num+1))

The complete sample code:

listr = [2343,  2913,  5466,  8589, 10151, 11713, 13275, 14837, 15618,7961, 19523, 21624, 21700]
start_num = min(listr)
end_num = max(listr)
new_listr = list(range(start_num, end_num+1))

Upvotes: 1

Sharad
Sharad

Reputation: 10592

Did you try:

>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,1)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]
>>> range(1,10,3)
[1, 4, 7]
>>> help(range)
Help on built-in function range in module __builtin__:

range(...)
    range(stop) -> list of integers
    range(start, stop[, step]) -> list of integers

    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.
(END)

Upvotes: 1

Related Questions