简体   繁体   English

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

[英]Construct a list of integers from bigger to smaller in python

I'm attempting to develop a little mouse controller app. 我正在尝试开发一个小鼠标控制器应用程序。 It should get (X, Y) coordinates and make the cursor go there. 它应该得到(X,Y)坐标并使光标移动到那里。

The problem is that when it attempts going to an X coordinate smaller than the current one. 问题是当它尝试进入小于当前坐标的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()

Above is just a very bad attempt to do so which does not give me the result I was looking for. 以上只是一个非常糟糕的尝试,这不会给我我想要的结果。 I was looking all over how to do it and just could not find the right way. 我一直在寻找如何做到这一点,只是找不到正确的方法。

In short, I wanna construct a list so it would go logically from bigger to smaller value, or smaller to bigger, according to paramerters order. 简而言之,我想构建一个列表,根据参数符号顺序,它将从逻辑上从大到小,或从更小到更大。

So, if I would give range(4, 1) which I want to be resulted in: [4, 3, 2] or range(1, 4), it would'nt mind and construct it the right way... 所以,如果我给出我希望得到的范围(4,1):[4,3,2]或范围(1,4),它就不会介意并以正确的方式构建它......

EDIT: I refactored the code according to the answers and made it more readable for other users to review. 编辑:我根据答案重构了代码,并让其他用户更容易阅读。 Note the "sequence" method in class MouseController: 注意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

either go of a step of -1 or 1 depending on which value is greater after you get the min and max: 获得最小值和最大值后,取决于哪个值更大,取得-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)

Output: 输出:

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]

If the min is greater we need a negative step, if not just use a step of 1. 如果min更大,我们需要一个消极的步骤,如果不是只使用1的步骤。

To make it a little more obvious how to use the logic in your own code you simply have to use the if/else: 为了更明显地如何在您自己的代码中使用逻辑,您只需使用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)

If you want to change the step size you can pass whatever n you want 如果你想改变的步长,你可以通过任何n你想

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] => [10,9,8,7,6,5,4,3]

with: 有:
lim1, lim2 = 2, 10 lim1,lim2 = 2,10

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

This form allow: 此表格允许:
list(range(lim1, lim2, 1 if lim1 列表(范围(lim1,lim2,1如果lim1

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM