How to select two elements from list and do math on them in Python?
Question: I have a file containing several start and end-positions of a sequence (MANY!)
and I want to select the start and end positions, subtract the end position
from the start to get the length of a particular sequence, and then randomly
place that sequence somewhere between the smallest start number and largest
end number. Don't know if that makes any sense.....

However here is what I have so far:

    
    
    #!/usr/bin/env python
    
    import sys
    import random
    import math
    import re
    
    file = open(sys.argv[1], 'r')
    
    list = []
    
    for line in file:
        if line.startswith('G'):
            continue
    
        else:
            pos = re.split('\s', line)
            list.append(pos[0])
            list.append(pos[1])
            list = map(int, list)
            for i in list:
                #ldom = (i:i+1)-i + 1 
    print randrange(1, 3000000000, ldom)
    
    file.close()
    

The '#' line is the one I'm having problems with. I have a list where each
element represents either a start or end position, list[0] represents a start
and list[1] an end position. I just don't know how to subtract list[1] from
list[0], and add 1 to that number....list comprehensions only seem to work on
non-specific elements or all.


Answer: Here's an ipython-interpreter example of splitting a line on whitespace (as
noted in docs for
[str.split()](http://docs.python.org/2/library/stdtypes.html#str.split),
whitespace is the default separator), converting items to integers (via
`map(int, ...)`, and then
[zipping](http://docs.python.org/2/library/functions.html#zip) into pairs:

    
    
    In [127]: line='0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16'
    
    In [128]: zip(*[iter(map(int, line.split()))]*2)
    Out[128]: [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, 11), (12, 13), (14, 15)]
    

You can set some variable, say `bounds`, to the zip expression, and then
iterate on list `bounds` to do whatever it is you need to do to the
“particular sequence”. For example:

    
    
    In [131]: bounds = zip(*[iter(map(int, line.split()))]*2)
    
    In [132]: for lo, hi in bounds: print 'lo:', lo, '\thi:', hi, '\tdelta:', hi-lo+1
    lo: 0   hi: 1   delta: 2
    lo: 2   hi: 3   delta: 2
    lo: 4   hi: 5   delta: 2
    lo: 6   hi: 7   delta: 2
    lo: 8   hi: 9   delta: 2
    lo: 10  hi: 11  delta: 2
    lo: 12  hi: 13  delta: 2
    lo: 14  hi: 15  delta: 2
    

