【问题标题】:Python - how do I extract a tuple from a list?Python - 如何从列表中提取元组?
【发布时间】:2021-11-04 15:25:41
【问题描述】:

我的变量points 中存储了两个坐标:[(100, 50)] 我正在尝试使用 pyautogui.moveTo(points) 移动鼠标,但出现错误:

pyautogui.PyAutoGUIException: The supplied sequence must have exactly 2 or exactly 4 elements (0 were received).

我假设这意味着我传递的是单个列表对象而不是坐标。

表达式 [(100, 50)] 是什么意思,我如何将 x 和 y 转换为两个元素。

我从中获得points 的源代码:

import cv2 as cv
import numpy as np

class Vision:

# properties
needle_img = None
needle_w = 0
needle_h = 0
method = None

# constructor
def __init__(self, needle_img_path, method=cv.TM_CCOEFF_NORMED):
    self.needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)

    # Save the dimensions of the needle image
    self.needle_w = self.needle_img.shape[1]
    self.needle_h = self.needle_img.shape[0]

    self.method = method

def find(self, haystack_img, threshold=0.5, debug_mode=None):
    # run the OpenCV algorithm
    result = cv.matchTemplate(haystack_img, self.needle_img, self.method)
    # Get the all the positions from the match result that exceed our threshold
    locations = np.where(result >= threshold)
    locations = list(zip(*locations[::-1]))

    for loc in locations:
        rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h]
        # Add every box to the list twice in order to retain single (non-overlapping) boxes
        rectangles.append(rect)
        rectangles.append(rect)
    # Apply group rectangles
    rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5)

    points = []
    if len(rectangles):

        line_color = (0, 255, 0)
        line_type = cv.LINE_4
        marker_color = (255, 0, 255)
        marker_type = cv.MARKER_CROSS

        # Loop over all the rectangles
        for (x, y, w, h) in rectangles:

            # Determine the center position
            center_x = x + int(w/2)
            center_y = y + int(h/2)
            # Save the points
            points.append((center_x, center_y))

            if debug_mode == 'rectangles':
                # Determine the box position
                top_left = (x, y)
                bottom_right = (x + w, y + h)
                # Draw the box
                cv.rectangle(haystack_img, top_left, bottom_right, color=line_color, 
                            lineType=line_type, thickness=2)
            elif debug_mode == 'points':
                # Draw the center point
                cv.drawMarker(haystack_img, (center_x, center_y), 
                            color=marker_color, markerType=marker_type, 
                            markerSize=40, thickness=2)

    if debug_mode:
        cv.imshow('Matches', haystack_img)

    return points

【问题讨论】:

  • “我知道 [ ] 用于列表或可变数据等可能更改的内容,而 ( ) 用于函数或元组,即不会更改的内容。”不是真的,可变性并不真正相关
  • 请在问题中包含您的源代码 - 谢谢。
  • 无论如何,在这种情况下,[(100, 50)] 是一个包含一个元组的列表...这是一个具有单个元素的序列(列表), (100, 50) 对(作为一个元组,它本身就是一个序列)
  • “我正在尝试使用 pyautogui.moveTo(points) 移动鼠标,但出现错误” - 是您在使用 pyautogui 时关于此错误的问题,还是关于语法[(100, 50)] 的含义?请将您的问题集中在您想要回答的一件具体事情上;如果是关于[(100, 50)] 的含义,那么您的实际问题与pyautogui 无关,并且您提到此错误只会使事情变得混乱。
  • 抱歉,两者都是。我想了解括号的含义,以帮助我回答我正在努力解决的问题。

标签: python pyautogui


【解决方案1】:

表达式 [(100, 50)] 是什么意思,如何将 x 和 y 转换为两个元素。

[...] 创建一个列表,其中包含您放入其中的任何内容。 (100, 50) 创建一个包含整数 100 和 50 的元组。所以你有一个列表,其中包含一个包含两个数字的元组。

我假设这意味着我传递的是单个列表对象而不是坐标。

你是对的,有点。问题不在于您要传递单个列表对象,您需要 传递单个列表 或更确切地说是序列 对象。问题是该列表对象仅包含 一个元素:元组。

您可以通过查看列表中的len 来检查:

>>> l = [(100, 50)]
>>> len(l)
1

您打算使用它的方式,pyautogui.moveTo(points) 想要一个包含 两个 元素的序列(列表或元组)。这些元素是您要移动到的点的坐标。

列表中的 tuple 就是这个二元素序列,所以这就是你需要传递的:

pyautogui.moveTo(points[0])

【讨论】:

  • 感谢@Pranav 的所有解释,我会记住len 的把戏。这给了我错误IndexError: list index out of range 每当它找不到东西并使脚本崩溃但我认为这是另一个问题/我需要研究的东西!
  • @L23P 当您尝试访问不存在的索引时会发生索引错误,在这种情况下,因为列表为空。检查列表是否为空非常简单,因为空序列在 python 中是错误的,所以你可以这样做 if points: moveTo(points[0])
  • 大声笑!我只是发布了一篇关于它的帖子,因为我尝试的方式略有不同!如果您想在此处声明另一个正确答案:stackoverflow.com/questions/69843672/…,因为这非常有效,谢谢!
  • 我投票关闭那个,因为它已经被回答了:) 一个空列表不是None,但它是错误的,所以points != None 不适用于points = [] @L23P
猜你喜欢
  • 1970-01-01
  • 2020-01-09
  • 1970-01-01
  • 2017-04-20
  • 2019-09-13
  • 1970-01-01
  • 2020-08-18
  • 2018-04-07
  • 1970-01-01
相关资源
最近更新 更多