我有 2 个这种形式的列表:
name = ['John','Jack','Benny', 'Jane']
location = [(219, 459, 374, 304)]
当我在做的时候
for (top, right, bottom, left), name in list(zip(location, name)):
print(location, name)
我得到以下结果:
[(219, 459, 374, 304)] J
但我想得到这样的结果:
[(219, 459, 374, 304)] John
此外,假设现在我的列表如下:
name = ['John','Jack', 'Jane']
location = [(219, 459, 374, 304), (200, 459, 350, 214), (159, 349, 264, 104)]
如何以这种形式得到上述for循环的结果:
[(219, 459, 374, 304)] John
[(200, 459, 350, 214)] Jack
[(159, 349, 264, 104)] Jane
请您参考如下方法:
你得到的原因
[(219, 459, 374, 304)] J
代替:
[(219, 459, 374, 304)] John
是因为你重用了name变量:
for (top, right, bottom, left), name in list(zip(location, name)):
print(location, name)
因此,当您第二次运行时,name 指的是您第一次尝试时的最后一个 name,大概是 'Jane'。所以第一个元素是 'J',因为它遍历 string。
小心你的变量名。并发布可重现的示例。如果您尝试按照建议在新流程中执行此操作,您将不会看到此行为。
