由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个python的小问题和一点感想
进入Programming版参与讨论
1 (共1页)
i****d
发帖数: 255
1
问题很小:
如果读取在一个窗口中鼠标点击的位置。
Google出来的答案五花八门,基本都是调用别的包,就是看不出来那个是最基本的函数,
x,y=leftclick()
之类的。可能是我没找着。
想着以前学C时,总有标准的基本的库函数可以查阅。Python的东西总好像被不同的人
包了一层又一层,看不出来最里头是什么东西。似乎总能找到一个方法实现,但总是不
知道这个方法是不是直接的,最好的。
望不吝赐教。
m******r
发帖数: 1033
2
我也有类似问题。想查个语法跑到PYDATA.ORG, 发现每个函数的解释都是又臭又长,本
来10分钟就能说完的事它在里面绕来绕去,恨不能写篇论文出来。 就说一个例子:
.WHERE(A,B) 本意是: IF A IS FALSE, THEN B.
NP.WHERE(A,B,C): IF A then B; otherwise C.
下面我摘自PYTHON的官网, 从头到尾就找不到正常人要找的东西。
https://pandas.pydata.org/docs/user_guide/indexing.html#the-where-method-and
-masking
The where() Method and Masking
Selecting values from a Series with a boolean vector generally returns a
subset of the data. To guarantee that selection output has the same shape as
the original data, you can use the where method in Series and DataFrame.
To return only the selected rows:
s[s > 0]
Out[185]:
3 1
2 2
1 3
0 4
dtype: int64
To return a Series of the same shape as the original:
s.where(s > 0)
Out[186]:
4 NaN
3 1.0
2 2.0
1 3.0
0 4.0
dtype: float64
Selecting values from a DataFrame with a boolean criterion now also
preserves input data shape. where is used under the hood as the
implementation. The code below is equivalent to df.where(df < 0).
df[df < 0]
Out[187]:
A B C D
2000-01-01 -2.104139 -1.309525 NaN NaN
2000-01-02 -0.352480 NaN -1.192319 NaN
2000-01-03 -0.864883 NaN -0.227870 NaN
2000-01-04 NaN -1.222082 NaN -1.233203
2000-01-05 NaN -0.605656 -1.169184 NaN
2000-01-06 NaN -0.948458 NaN -0.684718
2000-01-07 -2.670153 -0.114722 NaN -0.048048
2000-01-08 NaN NaN -0.048788 -0.808838
In addition, where takes an optional other argument for replacement of
values where the condition is False, in the returned copy.
df.where(df < 0, -df)
Out[188]:
A B C D
2000-01-01 -2.104139 -1.309525 -0.485855 -0.245166
2000-01-02 -0.352480 -0.390389 -1.192319 -1.655824
2000-01-03 -0.864883 -0.299674 -0.227870 -0.281059
2000-01-04 -0.846958 -1.222082 -0.600705 -1.233203
2000-01-05 -0.669692 -0.605656 -1.169184 -0.342416
2000-01-06 -0.868584 -0.948458 -2.297780 -0.684718
2000-01-07 -2.670153 -0.114722 -0.168904 -0.048048
2000-01-08 -0.801196 -1.392071 -0.048788 -0.808838
You may wish to set values based on some boolean criteria. This can be done
intuitively like so:
s2 = s.copy()
s2[s2 < 0] = 0
s2
Out[191]:
4 0
3 1
2 2
1 3
0 4
dtype: int64
df2 = df.copy()
df2[df2 < 0] = 0
df2
Out[194]:
A B C D
2000-01-01 0.000000 0.000000 0.485855 0.245166
2000-01-02 0.000000 0.390389 0.000000 1.655824
2000-01-03 0.000000 0.299674 0.000000 0.281059
2000-01-04 0.846958 0.000000 0.600705 0.000000
2000-01-05 0.669692 0.000000 0.000000 0.342416
2000-01-06 0.868584 0.000000 2.297780 0.000000
2000-01-07 0.000000 0.000000 0.168904 0.000000
2000-01-08 0.801196 1.392071 0.000000 0.000000
By default, where returns a modified copy of the data. There is an optional
parameter inplace so that the original data can be modified without creating
a copy:
df_orig = df.copy()
df_orig.where(df > 0, -df, inplace=True)
df_orig
Out[197]:
A B C D
2000-01-01 2.104139 1.309525 0.485855 0.245166
2000-01-02 0.352480 0.390389 1.192319 1.655824
2000-01-03 0.864883 0.299674 0.227870 0.281059
2000-01-04 0.846958 1.222082 0.600705 1.233203
2000-01-05 0.669692 0.605656 1.169184 0.342416
2000-01-06 0.868584 0.948458 2.297780 0.684718
2000-01-07 2.670153 0.114722 0.168904 0.048048
2000-01-08 0.801196 1.392071 0.048788 0.808838
Note
The signature for DataFrame.where() differs from numpy.where(). Roughly df1.
where(m, df2) is equivalent to np.where(m, df1, df2).
df.where(df < 0, -df) == np.where(df < 0, df, -df)
Out[198]:
A B C D
2000-01-01 True True True True
2000-01-02 True True True True
2000-01-03 True True True True
2000-01-04 True True True True
2000-01-05 True True True True
2000-01-06 True True True True
2000-01-07 True True True True
2000-01-08 True True True True
Alignment
Furthermore, where aligns the input boolean condition (ndarray or DataFrame)
, such that partial selection with setting is possible. This is analogous to
partial setting via .loc (but on the contents rather than the axis labels).
df2 = df.copy()
df2[df2[1:4] > 0] = 3
df2
Out[201]:
A B C D
2000-01-01 -2.104139 -1.309525 0.485855 0.245166
2000-01-02 -0.352480 3.000000 -1.192319 3.000000
2000-01-03 -0.864883 3.000000 -0.227870 3.000000
2000-01-04 3.000000 -1.222082 3.000000 -1.233203
2000-01-05 0.669692 -0.605656 -1.169184 0.342416
2000-01-06 0.868584 -0.948458 2.297780 -0.684718
2000-01-07 -2.670153 -0.114722 0.168904 -0.048048
2000-01-08 0.801196 1.392071 -0.048788 -0.808838
Where can also accept axis and level parameters to align the input when
performing the where.
df2 = df.copy()
df2.where(df2 > 0, df2['A'], axis='index')
Out[203]:
A B C D
2000-01-01 -2.104139 -2.104139 0.485855 0.245166
2000-01-02 -0.352480 0.390389 -0.352480 1.655824
2000-01-03 -0.864883 0.299674 -0.864883 0.281059
2000-01-04 0.846958 0.846958 0.600705 0.846958
2000-01-05 0.669692 0.669692 0.669692 0.342416
2000-01-06 0.868584 0.868584 2.297780 0.868584
2000-01-07 -2.670153 -2.670153 0.168904 -2.670153
2000-01-08 0.801196 1.392071 0.801196 0.801196
This is equivalent to (but faster than) the following.
df2 = df.copy()
df.apply(lambda x, y: x.where(x > 0, y), y=df['A'])
Out[205]:
A B C D
2000-01-01 -2.104139 -2.104139 0.485855 0.245166
2000-01-02 -0.352480 0.390389 -0.352480 1.655824
2000-01-03 -0.864883 0.299674 -0.864883 0.281059
2000-01-04 0.846958 0.846958 0.600705 0.846958
2000-01-05 0.669692 0.669692 0.669692 0.342416
2000-01-06 0.868584 0.868584 2.297780 0.868584
2000-01-07 -2.670153 -2.670153 0.168904 -2.670153
2000-01-08 0.801196 1.392071 0.801196 0.801196
where can accept a callable as condition and other arguments. The function
must be with one argument (the calling Series or DataFrame) and that returns
valid output as condition and other argument.
df3 = pd.DataFrame({'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]})
df3.where(lambda x: x > 4, lambda x: x + 10)
Out[207]:
A B C
0 11 14 7
1 12 5 8
2 13 6 9
x********2
发帖数: 1
3
这里假设你说的“窗口”是Windows里的窗口,
####################################################
#
# Prerequisites:
#
# 1. Install the package "pywin32" via the command "pip install pywin32"
#
# 2. Replace the value of the variable "g_target_window_title" with the
title of your target window
#
####################################################
import win32api, win32ui
# Please replace the value with the title of your target window
g_target_window_title = "appinst.d"
# Get the position of the mouse in the screen
(x_in_screen, y_in_screen) = win32api.GetCursorPos()
print("mouse position in screen: \n\n\tx=%d, y=%d\n" % (x_in_screen, y_in_
screen))
# Get the handle of the target window
wnd = win32ui.FindWindow(None, g_target_window_title)
# Get the position of mouse in the window's client area
(x_in_wnd, y_in_wnd) = wnd.ScreenToClient((x_in_screen, y_in_screen))
print("mouse position in the window's client area: \n\n\tx=%d, y=%d\n" % (x_
in_
wnd, y_in_wnd))
x********2
发帖数: 1
4
如果你喜欢C语言,那么微软的win32和MSDN是你的好朋友。
f*******t
发帖数: 7549
5
你要想获取其它程序窗口的点击,那就不知道了,python一般不是用来干这个的吧。如
果只是让python开一个窗口并获取点击事件,python自带一个叫turtle的lib,据说是
给kid玩的,接口很简单。不知道你是怎么搜的为什么一直找不到,我就在python doc
里搜mouse click,花了5分钟扫了一遍文档,2分钟装系统里缺的python-tk,并写出以
下算是working的prototype。
from turtle import *
def click(x, y):
print("Clicked:", x, y)
screen = Screen()
screen.onclick(click)
screen.mainloop()
输出:
Clicked: -203.0 -125.0
Clicked: -320.0 190.0
Clicked: 3.0 198.0
Clicked: 256.0 54.0
Clicked: 403.0 -345.0
Clicked: 403.0 -345.0
Clicked: -136.0 -350.0
i****d
发帖数: 255
6
谢谢!
这里的窗口是泛指的,可以是系统的窗口像是浏览器、terminal之类的,也可以是
python运行中自己新开的一个图形窗口,不限于Windows系统。
看了一圈,有你说的这个,也有pyautogui之类的。后者尤甚,一层层包着。

【在 x********2 的大作中提到】
: 这里假设你说的“窗口”是Windows里的窗口,
: ####################################################
: #
: # Prerequisites:
: #
: # 1. Install the package "pywin32" via the command "pip install pywin32"
: #
: # 2. Replace the value of the variable "g_target_window_title" with the
: title of your target window
: #

i****d
发帖数: 255
7
谢谢!这个tturtle我知道,说的也是这个事情:为什么要把这样一个基本的鼠标操作
包到另外一个lib里面。
当然你说的可能是对的,python不是用来干这个的。

doc

【在 f*******t 的大作中提到】
: 你要想获取其它程序窗口的点击,那就不知道了,python一般不是用来干这个的吧。如
: 果只是让python开一个窗口并获取点击事件,python自带一个叫turtle的lib,据说是
: 给kid玩的,接口很简单。不知道你是怎么搜的为什么一直找不到,我就在python doc
: 里搜mouse click,花了5分钟扫了一遍文档,2分钟装系统里缺的python-tk,并写出以
: 下算是working的prototype。
: from turtle import *
: def click(x, y):
: print("Clicked:", x, y)
: screen = Screen()
: screen.onclick(click)

f*******t
发帖数: 7549
8
不管什么语言,UI肯定要跟系统交互,不同的系统有不同的api,想提供一个跨平台兼
容的interface肯定要调用某种中间层library

【在 i****d 的大作中提到】
: 谢谢!这个tturtle我知道,说的也是这个事情:为什么要把这样一个基本的鼠标操作
: 包到另外一个lib里面。
: 当然你说的可能是对的,python不是用来干这个的。
:
: doc

n******t
发帖数: 4406
9
想想这个问题,C里面如果做这件事?
是不是要先问操作系统是什么?用什么库?
python也是一样,要不就是分OS,要不就是你的跨平台库帮你解决了这个事情。所以还
是先问那两个问题再说。

数,

【在 i****d 的大作中提到】
: 问题很小:
: 如果读取在一个窗口中鼠标点击的位置。
: Google出来的答案五花八门,基本都是调用别的包,就是看不出来那个是最基本的函数,
: x,y=leftclick()
: 之类的。可能是我没找着。
: 想着以前学C时,总有标准的基本的库函数可以查阅。Python的东西总好像被不同的人
: 包了一层又一层,看不出来最里头是什么东西。似乎总能找到一个方法实现,但总是不
: 知道这个方法是不是直接的,最好的。
: 望不吝赐教。

1 (共1页)
进入Programming版参与讨论