簡體   English   中英

比較和替換嵌套列表的元素

[英]comparing and replacing the elements of a nested list

我有一個列表是:

mylist = {
           '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)]
          }

我必須得到如下輸出:

 mylist = {
           'a': [(0.3, 0.35), (0.2,0.4)] 
           'b': [(0.3,0.35), (0.05,0.15)]
           'c': [(0.15,0.35), (0.05,0.15)]
           'd': [(0.15,0.35), (0.05,0.15)]
          }

上面的列表在我打印時看起來像這樣,

mylist = [ ('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) ] ) ]

現在的算法如下:

1st iteration: Compare a[0] and b[0] ie (-1, -1) and (0.3, 1.0). 
               Here replace (-1, -1) by (0.3, 1.0). 
               Rule: (-1, -1) is considered as empty or not in use so 
                     it gets replaced while comparison. 

               Similarly, compare a[1] and b[1] ie (0.2, 0.4) and (-1, -1). 
               Here keep the same value as b[1] is empty so no change. 

               Hence the new elements of 'a' are (0.3, 1.0), (0.2, 0.4). 
               Rule: if comparing with empty one then keep the same values.

2nd iteration: Compare new values of a[0] and c[0] ie (0.3, 1.0) and (-1, -1). 
               Here again no change. 
               Similarly, compare new values of a[1] and c[1] ie (0.2, 0.4) and (-1, -1).
               Here also no change. 
               Now the new elements of 'a' are (0.3, 1.0), (0.2, 0.4).

此過程一直進行到將“ a”與列表中的最后一個元素(直到“ d”)進行比較為止。 然后是“ b”的轉彎,相同的事物將在“ b”和“ c”之間繼續,然后是“ b和” d”,依此類推。

在兩個實際范圍(0.1,0.3)和(0.5,1.0)之間進行比較時的其他規則。

假設兩個范圍完全重疊,如(0.1,0.8)和(0.3,0.9),則它們之間的共同點應該是(0.3,0.8)。

如果它們不像(0.1,0.4)和(0.5,0.9)那樣重疊,則應選擇自己的(0.1,0.4)。

如果它們部分重疊,那么它們之間也可以共用。 像(0.4,1.0)和(0.8,1.5)一樣,它應該選擇(0.8,1.0)。

PS值(0.2,0.4)是實際指示的范圍,實際值將在0.2到0.4之間變化。 我想現在我已經解釋得更清楚了。 謝謝

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()

編輯:更新了update_ranges以使其可用於任意數量的列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM