[Tutor] Limitation of range() function in Walking problem

Luke Paireepinart rabidpoobear at gmail.com
Sat Sep 16 08:57:33 CEST 2006


Srinivas Iyyer wrote:
> Dear tutors:
>
> this question is in continuation of my previous
> question about list manipulations where Kent and Bob
> helped me. 
> Thanks to you both. 
>
> My aim was to walk along coordinates of exon of a gene
> on a chromosome. In simple terms:
>
>
> Coordinates of a variable G1:
> a1 : 3-8
> b2 : 10-25
> c3 : 7-18
> d4 : 10-13
>
> Now, since these coordinates are in ranges of others,
> I can define the extreames of the variables, which are
> 3 and 25. 
>
> Example pictorially:
>
> 3-----8
>         10---------------25
>     7-------------18
>         10---13
>
>
> Now, I have variables G1....Gn.  Each variable may
> have individual pieces a1(n),b2(n),c3(n).....z(n).
>
> my kind of solution, which is very crappy (i assume):
>
> Sort the list:
> [3,8]                 [3,8]
> [10,25]     sort      [7,18]
> [7,18]     -------->  [10,13]
> [10,13]               [10,25]
>
> I loop through each list and check if list[0] is in
> range of previous list list[1] AND if it is less or
> greater than. If less, I retain the greater number and
> loop through. 
>
> The question to the forum is, I am using range()
> function to solve this. Since the numbers in the
> example are small, it worked. in the real world
> example , it did not work, because computer slowed
> down and eventually crashed.  The range(81393417) is
> really big. 
>
> question:
> 1. What to do If I have to check if a number is in
> range of 81393417.  
>   
> 2. Is there any alternative.
>   
The function 'range()' generates a list in memory of all the integers 
from 0 to n.
This is a lot of memory, as you can probably tell.
Try using an iteration function.
example: 'xrange()'
That won't generate the intermediate list that the for loop loops over,
so it will be slightly slower over each iteration (since the function 
'xrange()' is called
every loop and it returns the next number) but it won't eat up all that 
memory
> 3. is solution that I have, is it appropriate. 
>   
I don't know, I'm in the middle of writing some code and I don't want to
forget what I was doing, so I didn't read any of yours.
Hope the xrange tip helps, though.
Another alternative is to use a while loop and a control variable
that you just increment on each loop iteration.
I'm guessing the reason it's crashing is because you're running out of 
memory
and the computer's getting mad at you.
-Luke
> Please help. 
>
>
> My real world example:
>
>
> kd ={'GeneID:1519':
> ['81393417\t81395369','81397635\t81397727','81398841\t81399004','81406661\t81406782','81408670\t81408837','81411616\t81411755','81412455\t81412563','81423012\t81423195'],
>        
> 'GeneID:445347':['37788202\t37788789','37790730\t37790777','37793908\t37794237','37802146\t37802206''37788202\t37788789','37790730\t37790777','37793908\t37794237','37802146\t37802206']}
>
> kk = ['GeneID:1519','GeneID:1519','GeneID:445347']
>
>
> for i in kk:
>         if kd.has_key(i):
>                 pairs = [map(int,x.split('\t'))for x
> in kd[i]]
>                 pairs.sort()
>                 i = iter(pairs)
>                 last = i.next()
>                 for cur in pairs:
>                         if cur[0] in range(last[1]):
>                                 if cur[1] > last[1]:
>                                         last =
> [last[0],cur[1]]
>                                 else:
>                                         last =
> [last[0],last[1]]
>                         else:
>                                 print last
>                                 cur = last
>                 print last
>
>
>
> Thanks
>
>
>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   



More information about the Tutor mailing list