簡體   English   中英

在python中構造一個從大到小的整數列表

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

我正在嘗試開發一個小鼠標控制器應用程序。 它應該得到(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()

以上只是一個非常糟糕的嘗試,這不會給我我想要的結果。 我一直在尋找如何做到這一點,只是找不到正確的方法。

簡而言之,我想構建一個列表,根據參數符號順序,它將從邏輯上從大到小,或從更小到更大。

所以,如果我給出我希望得到的范圍(4,1):[4,3,2]或范圍(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]

如果min更大,我們需要一個消極的步驟,如果不是只使用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你想

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,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