【问题标题】:Construct a list of integers from bigger to smaller in python在python中构造一个从大到小的整数列表
【发布时间】:2015-10-01 01:21:51
【问题描述】:

我正在尝试开发一个小鼠标控制器应用程序。它应该获取 (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

【问题讨论】:

  • 提示:range(4,1,-1)
  • 感谢@NightShadeQueen,但我应该更清楚地说明我的问题:“4”和“1”将是变量,我们需要它在逻辑上知道如何构建列表。因此,如果我给它 range(4, 1) 或 range(1, 4),它不会介意并以正确的方式构造它...

标签: python list foreach rounding reverse


【解决方案1】:

range(a, b, -1 if a > b else 1)

【讨论】:

    【解决方案2】:
    lim1, lim2 = 10, 2  
    step = 1 if lim1<lim2 else -1  
    lst = list(range(lim1, lim2, step))  
    print(lst)  
    

    => [10, 9, 8, 7, 6, 5, 4, 3]

    与:
    lim1, lim2 = 2, 10

    => [2, 3, 4, 5, 6, 7, 8, 9]

    此表格允许:
    列表(范围(lim1,lim2,如果 lim1,则为 1

    【讨论】:

    • 是的,'min'/'max' 除外。我们不需要它们。不?然后我们可以使用:list(range(lim1, lim2, 1 if lim1
    • 嗯,是的。整数的 min(a) 很奇怪。不?因此,更容易编写更短的形式:list(range(lim1, lim2, 1 if lim1
    • 没有。但是 TheSilence 的问题是关于整数的。他说:“所以,如果我给出 range(4, 1),我希望它的结果是:[4, 3, 2]”
    • 编辑您的答案并获得绿色 V ;)
    【解决方案3】:

    根据获得最小值和最大值后哪个值更大,步长为 -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

    【讨论】:

    • 谢谢@Padraic Cunningham,但没有更清洁的方法吗?
    • @TheSilence,唯一添加的代码是 step = -1 if mn &gt; mx else 1,我使用了 min 和 max,因为这就是你正在使用的
    • @TheSilence “没有更清洁的方法吗?” - 3 行函数非常简洁和优雅(不要忘记:根据您自己非常的特定需求量身定制西装!)。但是,嘿,如果您想出更好的方法 - 请在此处发布作为答案 - 我很想学习新的东西!
    • 你可以变短,但我怀疑不是不重复你自己。 range(mn,mx,abs(mx-mn)//(mx-mn)) 浮现在脑海中,但是,真的,不要这样做。
    • 范围(a, b, -1 if a > b else 1)
    猜你喜欢
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    • 2019-09-08
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    相关资源
    最近更新 更多