python - 在python中构造一个从大到小的整数列表

标签 python list foreach rounding reverse

我正在尝试开发一个小鼠标 Controller 应用程序。它应该获取 (X, Y) 坐标并使光标移动到那里。

问题在于,当它尝试转到小于当前坐标的 X 坐标时。

import win32con
from win32api import GetCursorPos, SetCursorPos, mouse_event, GetSystemMetrics
from time import sleep

def clickWithCursor(xDesired, yDesired):
    xCurrent, yCurrent = GetCursorPos()

slope = float(yDesired - yCurrent) / float(xDesired - xCurrent)

def goAhead(x, y):
    for x in range(min(xCurrent, xDesired), max(xDesired, xCurrent), 2):
        y = int(slope * (x - xCurrent) + yCurrent)
        SetCursorPos((int(x), y))
        sleep(0.002)

    mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
    mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

return goAhead(0, 0)

def main():
    clickWithCursor(243, 184)

main()

以上只是一次非常糟糕的尝试,它没有给我想要的结果。我一直在寻找如何去做,就是找不到正确的方法。

简而言之,我想构建一个列表,以便根据参数顺序从逻辑上从大值到小值,或从小值到大。

因此,如果我给出 range(4, 1),我希望结果为:[4, 3, 2] 或 range(1, 4),它不会介意并以正确的方式构造它。 ..

编辑: 我根据答案重构了代码,并使其更具可读性以供其他用户查看。注意 MouseController 类中的“sequence”方法:

from win32con import MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP
from win32api import GetCursorPos, SetCursorPos, mouse_event
from time import sleep


class CursorPositionPrinter(object):
    """docstring for CursorPositionPrinter"""
    def print_cursor_pos(self):
        print GetCursorPos()

    def __init__(self):
        super(CursorPositionPrinter, self).__init__()


class AutoClicker(object):
    """docstring for AutoClicker"""
    def click(self, times):

        xCurrent, yCurrent = GetCursorPos()
        for i in xrange(times):
            self.simple_click(xCurrent, yCurrent)

    def simple_click(self, x, y):
        mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0)

    def __init__(self):
        super(AutoClicker, self).__init__()


class MouseController(CursorPositionPrinter, AutoClicker):
    """docstring for MouseController
    Controlls your mouse magically!!!"""

    def sequence(self, a, b, n):
        mn, mx = a, b
        step = -n if mn > mx else n

        for value in xrange(mn, mx, step):
            yield value

    def click_with_cursor(self, xDesired, yDesired):
        self.go_to_coordinates(xDesired, yDesired)
        self.simple_click(xDesired, yDesired)

    def go_to_coordinates(self, xDesired, yDesired):

        xCurrent, yCurrent = GetCursorPos()

        slope = float(yDesired - yCurrent) / float(xDesired - xCurrent)

        for x in self.sequence(xCurrent, xDesired, 2):
            y = int(slope * (x - xCurrent) + yCurrent)
            SetCursorPos((int(x), y))
            sleep(self.latency)

        SetCursorPos((xDesired, yDesired))

    def __init__(self, latency=0.02):
        super(MouseController, self).__init__()
        self.latency = latency

最佳答案

在获得最小值和最大值后,根据哪个值较大,选择 -1 或 1 的步长:

def up_down(a, b):
    mn, mx = min(a), max(b)
    step = -1 if mn > mx else 1
    return range(mn, mx, step)

输出:

In [9]: list(up_down([4,5,5,7],[0,1]))
Out[9]: [4, 3, 2]

In [10]: list(up_down([0,1],[4,5,5,7] ))
Out[10]: [0, 1, 2, 3, 4, 5, 6]

如果最小值更大,我们需要一个负步长,如果不是只使用 1 的步长。

为了使如何在您自己的代码中使用逻辑更加明显,您只需使用 if/else:

def goAhead(x, y,n=1):
    step = -n if xCurrent > xDesired else n
    for x in range(xCurrent, xDesired, step):
        y = int(slope * (x - xCurrent) + yCurrent)
        SetCursorPos((int(x), y))
        sleep(0.002)

如果你想改变步长,你可以传递任何你想要的n

关于python - 在python中构造一个从大到小的整数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31372177/

相关文章:

javascript - 有一种简单的方法可以确保 JavaScript 中 forEach/for 循环的正确顺序吗?

node.js - 在 forEach 之后发送响应

python - Django 可重用应用程序和迁移

python - 从 python 中的文件中删除行的有效方法(并保持相同的文件名)?

python - NumPy:将索引选择功能应用于多个轴

list - prolog 列表中某个元素在列表中出现了多少次?

javascript - JS - 数组 forEach 方法说明

python - 如何在 scikit-learn 中使用管道调整自定义内核函数的参数

javascript - 在 Javascript 中存储带有散列的巨大列表的最佳方法

python - 有没有办法读取字符串并将字符串中的每个字母转换为另一个字符串中的项目?