这是我尝试使用该map函数的循环:
volume_ids = [1,2,3,4,5]
ip = '172.12.13.122'
for volume_id in volume_ids:
my_function(volume_id, ip=ip)
Run Code Online (Sandbox Code Playgroud)
有没有办法可以做到这一点?如果它不是ip参数,那将是微不足道的,但我不知道如何处理它.
我有一个带args和kwargs的函数,我需要在我的装饰器中根据函数中第二个 arg 的值做一些事情,如下面的代码所示:
def workaround_func():
def decorator(fn):
def case_decorator(*args, **kwargs):
if args[1] == 2:
print('The second argument is a 2!')
return fn(*args, **kwargs)
return case_decorator
return decorator
@workaround_func()
def my_func(arg1, arg2, kwarg1=None):
print('arg1: {} arg2: {}, kwargs: {}'.format(arg1, arg2, kwarg1))
Run Code Online (Sandbox Code Playgroud)
问题是python允许用户使用第二个参数作为常规参数或关键字参数调用该函数,因此如果用户my_func使用arg2kwarg 调用它,它会引发一个IndexError,见下文:
In [8]: d.my_func(1, 2, kwarg1=3)
The second argument is a 2!
arg1: 1 arg2: 2, kwargs: 3
In [9]: d.my_func(1, arg2=2, kwarg1=3)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-9-87dc89222a9e> in …Run Code Online (Sandbox Code Playgroud) 我试图通过在二维数组上应用操作来改善numpy性能,问题是数组中每个元素的值取决于该元素的i,j位置.
显然,这样做的简单方法是使用嵌套的for循环,但我想知道是否有更好的方法可以引用np.indices或沿着这些行的东西?这是我的"愚蠢"代码:
for J in range(1025):
for I in range(1025):
PSI[I][J] = A*math.sin((float(I+1)-.5)*DI)*math.sin((float(J+1)-.5)*DJ)
P[I][J] = PCF*(math.cos(2.*float(I)*DI)+math.cos(2.*float(J)*DJ))+50000.
Run Code Online (Sandbox Code Playgroud) 我正在尝试开始一个time.Ticker偶数时间戳。基本上我想要的是这段代码:
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(time.Second * 5)
for i := 0; i < 2; i++ {
select {
case <-ticker.C:
fmt.Println("Time: ", time.Now().Round(time.Second*1))
}
}
ticker.Stop()
}
Run Code Online (Sandbox Code Playgroud)
要始终以偶数 5 秒间隔打印:
Time: 2015-10-21 12:53:50 -0600 MDT
Time: 2015-10-21 12:53:55 -0600 MDT
Run Code Online (Sandbox Code Playgroud)
有一个优雅的解决方案吗?
python ×3
arguments ×1
arrays ×1
decorator ×1
go ×1
indexing ×1
map-function ×1
numpy ×1
performance ×1