Python 减去字符串中的数字

Python 减去字符串中的数字,python,coordinates,subtraction,pyautogui,Python,Coordinates,Subtraction,Pyautogui,我有两个坐标(x,y),我想把它们相互减去 您可以忽略它在pyautogui中,但如何减去test1-test2呢 import pyautogui test1 = pyautogui.locateCenterOnScreen("test1.png",confidence=0.8) test2 = pyautogui.locateCenterOnScreen("test2.png",confidence=0.9) print(test1) prin

我有两个坐标(x,y),我想把它们相互减去

您可以忽略它在pyautogui中,但如何减去test1-test2呢

import pyautogui

test1 = pyautogui.locateCenterOnScreen("test1.png",confidence=0.8)

test2 = pyautogui.locateCenterOnScreen("test2.png",confidence=0.9)

print(test1)

print(test2)

>>>Point(x=1072, y=543)

>>>Point(x=1304, y=689)
建议
test1
test2
值是Python元组,而不是
Point
对象:

locateCenterOnScreen()
函数只返回屏幕上图像中间的XY坐标:

>>> pyautogui.locateCenterOnScreen('looksLikeThis.png')  # returns center x and y
(898, 423)
要获取元素,可以使用
[0]
[1]
等来获取第一个、第二个和后续元素

要计算两个元组的差异,您可以在
test1
test2
中的每对元素上运行:

diff = tuple(map(lambda i, j: i - j, test1, test2))
请注意,元组的顺序将改变结果。
test1-test2
的结果将不同于
test2-test1
。因此,您可能希望了解差异的原因:

abs_diff = tuple(map(lambda i, j: abs(i - j), test1, test2))

我认为您可以将Point对象强制转换为列表

test1 = list(test1)
然后使用以下命令访问X或Y坐标:

print(f"X coordinate: {test1.x}")
print(f"Y coordinate: {test1.y}")

类似于
test1[0]-test2[0]
的东西会让你得到x I assumehollo@Michal,欢迎。以后,请把你的文章编排得更好。请参阅如何在此处插入代码的语法突出显示。它不起作用,因为未定义x和y,但感谢您的回答。好的,它起作用了。多谢各位