我已经生成了此函数,并且正在传递一些输入参数(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)
答案 0 :(得分: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'>
这是您想知道的吗?