【发布时间】:2019-08-24 03:14:18
【问题描述】:
我已经生成了这个函数,我正在传递一些输入参数(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)
【问题讨论】:
标签: python function input tuples