BioInformatician
BioInformatician

Reputation: 97

Dividing Number into Segments of knowing Ranges using Python

I have a number like 9970024 that comes from a total number of 249250621 and I would like to divide the 9970024 number into segments of equal range. That is, 0-9970024 is one iteration that I want to do and then next 9970024 +1 to 19940049 (from 9970025+9970024) until it reaches to the total number which is 249250621. How can I do such thing using python.

My initial try is as follows:

j = 0
x = 9970024
total = 249250621
while (j <= total):
 for i in range(j,x):
  j = j +1
 j = i+1
 x = x+j

Upvotes: 0

Views: 1544

Answers (3)

Falko
Falko

Reputation: 17887

Let's use some smaller numbers for clarity.

total = 25
fragment = 10
for start in xrange(0, total, fragment):
    print "start = ", start
    for i in range(start, min([start + fragment, total])):
        print i

Output:

start = 0
0
1
2
3
4
5
6
7
8
9
start = 10
10
11
12
13
14
15
16
17
18
19
start = 20
20
21
22
23
24

Upvotes: 1

Martin Evans
Martin Evans

Reputation: 46779

An alternative way of looking at this could be to simply iterate the whole range but determine when a segment boundary is reached.

total = 249250621       
segment = total / 25        # 9970024 

for j in xrange(0, total+1):
    if j % segment == 0:
        print "%u - start of segment" % j

Upvotes: 1

vks
vks

Reputation: 67978

for i in range(0,249250621,9970024):
    for j in range(i,i+9970024+1):
        pass

You can try this.Here i will increase by 9970024 everytime.And j will loop for 9970024 times.

Upvotes: 0

Related Questions