簡體   English   中英

遍歷元組列表時的迭代問題

[英]Iteration Issues When Looping Through A List of Tuple

我已經生成了此函數,並且正在通過一些輸入參數(i,j,t),並且對此做了一些處理。 現在,如果您查看輸出,您將看到我有一些打印功能分別標記為Test1和Test2,而令我感到困惑的是,為什么這兩個測試之間存在差異,因為從我的角度來看,它們應該打印出相同的顏色輸入(1,1,1)的事物。

第一輪測試的兩個結果是相似的,這很棒。 但是,經過測試之后,我輸入的輸入參數似乎從(1,1,1)更新為(2,2,1)。 這個奇怪的更新似乎也發生在第一個for循環的開始,我希望嘗試理解為什么發生此更新。

我真的不需要固定的代碼,因為在函數的開頭引入x_coord和y_coord變量似乎可以緩解我遇到的問題並按我的意願去做。 但是,如果有人可以幫助我理解為什么“ i”和“ j”輸入發生這種更新,以及為什么測試打印語句不相同,那太好了!

model = ConcreteModel()

Imax = 3
Jmax = 3
Tmax = 1

model.Iset = RangeSet(1,Imax) #e.g. i = {1, 2, 3}
model.Jset = RangeSet(1,Jmax)
model.Tset = RangeSet(1,Tmax)
model.immigration = Var(model.Iset, model.Jset, model.Tset, initialize = 1)
model.inf_b4treat = Var(model.Iset, model.Jset, model.Tset, initialize=1)
model.foo = Var(model.Iset, model.Jset, model.Tset, initialize=1)



def get_neighbours(i,j,t): 

    x_coord = i
    y_coord = j
    rowbound = Imax + 1
    colbound = Jmax + 1

    neighbours = [
        (i - 1, j - 1, t), (i - 1, j, t), (i - 1, j + 1, t),
        (i, j - 1, t), (i, j + 1, t),
        (i + 1, j - 1, t), (i + 1, j, t), (i + 1, j + 1, t),
    ]
    print(f"Neighbours of cell ({i},{j},{t}): {neighbours}")
    print("")

    valid_tuples = []
    invalid_tuples = []
    # for each tuple in neighbours, we will print out the good and bad coordinates.

    print(f"First.Test1 = ({i},{j},{t})")
    print(f"First.Test2 = ({x_coord},{y_coord},{t})")
    print("")

    for (i,j,t) in neighbours:
        if not 0 < i < rowbound or not 0 < j < colbound:
            #print(f"Invalid Tuple --> ({i},{j},{t})")
            invalid_tuples.append((i,j,t))

        else:
            #print(f"Valid Tuple --> ({i},{j},{t})")
            valid_tuples.append((i,j,t))

    print(f"Second.Test1 = ({i},{j},{t}) ")
    print(f"Second.Test2 = ({x_coord},{y_coord},{t})")
    print("")

    print(f"Invalid Tuples: {invalid_tuples}")   
    print(f"Valid Tuples:   {valid_tuples}")
    print("")

    immigration_value_ijt = 0
    for (i,j,t) in valid_tuples:
        immigration_value_ijt += value(model.inf_b4treat[i,j,t])
    print(f"Quantity Immigrating to cell.Test1 ({i},{j},{t}): {immigration_value_ijt}")        
    print(f"Quantity Immigrating to cell.Test2 ({x_coord},{y_coord},{t}): {immigration_value_ijt}")
    print("")
    print(f"Third.Test1 = ({i},{j},{t})")
    print(f"Third.Test2 = ({x_coord},{y_coord},{t})")
    print("")    

get_neighbours(1,1,1)

輸出:

Neighbours of cell (1,1,1): [(0, 0, 1), (0, 1, 1), (0, 2, 1), (1, 0, 1), (1, 2, 1), (2, 0, 1), (2, 1, 1), (2, 2, 1)]

First.Test1 = (1,1,1)
First.Test2 = (1,1,1)

Second.Test1 = (2,2,1) 
Second.Test2 = (1,1,1)

Invalid Tuples: [(0, 0, 1), (0, 1, 1), (0, 2, 1), (1, 0, 1), (2, 0, 1)]
Valid Tuples:   [(1, 2, 1), (2, 1, 1), (2, 2, 1)]

Quantity Immigrating to cell.Test1 (2,2,1): 3
Quantity Immigrating to cell.Test2 (1,1,1): 3

Third.Test1 = (2,2,1)
Third.Test2 = (1,1,1)

您正在用for循環遮蓋i,j,t,並且該循環在每次迭代時都會設置新值。 因此,最終結果是鄰居列表的最后一個條目。

以下是一些打印語句,顯示正在發生的情況:

def test_func(a, b, c):
    l1 = [(0, 0, 1), (0, 1, 2), (2, 2, 1)]
    print(f'Before a {a} id {id(a)}, b {b} id {id(b)}, c {c} id {id(c)}\n')
    # since three variable are specified, each tuple entry is unpacked
    # into the variables, which are a, b and c
    for (a, b, c) in l1:  # The parenthesis don't do anything and aren't needed
        print(f'Inside a {a} id {id(a)}, b {b} id {id(b)}, c {c} id {id(c)}')
        print(f'Inside a is type {type(a)}')
    print(f'\nAfter a {a} id {id(a)}, b {b} id {id(b)}, c {c} id {id(c)}')

    # just a single variable name would be the entire tuple
    for entry_tuple in l1:  # The parenthesis don't do anything and aren't needed
        print(f'Entry tuple is {entry_tuple}  and the type is {type(entry_tuple)}')

    print(f'At end a is type {type(a)}')


test_func(1, 1, 1)

輸出:

Before a 1 id 94439557494624, b 1 id 94439557494624, c 1 id 94439557494624

Inside a 0 id 94439557494592, b 0 id 94439557494592, c 1 id 94439557494624
Inside a is type <class 'int'>
Inside a 0 id 94439557494592, b 1 id 94439557494624, c 2 id 94439557494656
Inside a is type <class 'int'>
Inside a 2 id 94439557494656, b 2 id 94439557494656, c 1 id 94439557494624
Inside a is type <class 'int'>

After a 2 id 94439557494656, b 2 id 94439557494656, c 1 id 94439557494624
Entry tuple is (0, 0, 1)  and the type is <class 'tuple'>
Entry tuple is (0, 1, 2)  and the type is <class 'tuple'>
Entry tuple is (2, 2, 1)  and the type is <class 'tuple'>
At end a is type <class 'int'>

那是你想知道的嗎?

暫無
暫無

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

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