我想知道是否可以根据全局设置(例如操作系统)控制 Python 函数定义。例子:
@linux
def my_callback(*args, **kwargs):
print("Doing something @ Linux")
return
@windows
def my_callback(*args, **kwargs):
print("Doing something @ Windows")
return
Run Code Online (Sandbox Code Playgroud)
然后,如果有人使用 Linux,my_callback将使用第一个定义,第二个将被默默忽略。
它不是关于确定操作系统,而是关于函数定义/装饰器。
我有一个列表X和一个列表Y,其中有一些洗牌索引.
X = ['a', 'b', 'c', 'd','e']
Y = [ 1 , 3 , 4 , 0 , 2 ]
Run Code Online (Sandbox Code Playgroud)
我想有一个新的列表Z,使得
Z = [ X[i] for i in Y ] = ['b', 'd', 'e', 'a', 'c']
Run Code Online (Sandbox Code Playgroud)
问题是我必须多次这样做,因为这么大arrays.有什么比循环列表更有效的方法?
注意: numpy解决方案是相关的!