python - 循环遍历元组列表时的迭代问题
问题描述
我已经生成了这个函数,我正在传递一些输入参数(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'>
这就是你想知道的吗?
推荐阅读
- node.js - 使用 cli 命令在打包的电子应用程序中使用 node_modules
- javascript - 将表单信息发送到服务器的最佳实践
- java - Vera 代码报告中的加密算法违规
- java - 如何生成 Java PKCS12 存储以连接到常规 SSL 网站?
- windows - 让我保持登录功能如何在 Windows/桌面应用程序中工作
- javascript - 在选择下拉列表中更改时无法调用 javascript 函数
- google-cloud-run - 向 Cloud Run 中的内部方法添加跟踪
- azure - 如何在 Win 10 x86 位 VM 中配置 Azure 虚拟机代理?
- javascript - 带有服务器端分页的角度js数据表标识为空
- c# - Unity 不更新对象 transform.position