Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def update(mylist, row, col, cmprow, cmpcol):
- lo, hi = mylist[row][col]
- low, high = mylist[cmprow][cmpcol]
- # always replace the current value if it's (-1, -1)
- if (lo, hi) == (-1, -1):
- mylist[row][col] = low, high
- print "replacing empty", row, col, "with", cmprow, cmpcol
- return
- # never replace the current value if the ranges don't overlap
- # or the other range is (-1, -1)
- if (low, high) == (-1, -1) or lo >= high or hi <= low:
- print row, col, "doesn't overlap", cmprow, cmpcol
- return
- # set the low to the highest low and the high to the lowest high
- print "updating", row, col, "with", cmprow, cmpcol
- mylist[row][col] = max((lo, low)), min((hi, high))
- def update_ranges(oldlist):
- # make a copy of the list as we're going to modify it
- mylist = oldlist[:]
- # we don't need the row titles, they just complicate things
- rowtitles, mylist = zip(*mylist)
- rows = len(mylist)
- columns = range(len(mylist[0]))
- # for each row except the last
- for i in xrange(rows - 1):
- # update it by going down all the rows below it
- for k in xrange(i+1, rows):
- # for both columns
- for j in columns:
- update(mylist, i, j, k, j)
- # put the row titles back in
- mylist = zip(rowtitles, mylist)
- return mylist
- def test():
- oldlist = [ ('a', [ (-1, -1), (0.2, 0.4) ] ),
- ('b', [ (0.3, 1.0), (-1, -1) ] ),
- ('c', [ (-1, -1), (-1, -1) ] ),
- ('d', [ (0.15, 0.35), (0.05, 0.15) ] ) ]
- print "Original List"
- print '\n'.join(str(l) for l in oldlist)
- newlist = update_ranges(oldlist)
- print "New List"
- print '\n'.join(str(l) for l in newlist)
- if __name__ == '__main__':
- test()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement