小编Nil*_*aha的帖子

在每个元素上使用条件的 Numpy 过滤器

我有一个过滤器表达式如下:

feasible_agents = filter(lambda agent: agent >= cost[task, agent], agents)

agentspython 列表在哪里。

现在,为了加快速度,我正在尝试使用 numpy 来实现这一点。

使用 numpy 的等价物是什么?

我知道这有效:

threshold = 5.0
feasible_agents = np_agents[np_agents > threshold]
Run Code Online (Sandbox Code Playgroud)

np_agents的 numpy 等价物在哪里agents

但是,我希望阈值是 numpy 数组中每个元素的函数。

python numpy

8
推荐指数
2
解决办法
1万
查看次数

Python:将额外的参数传递给 callable

我有以下python代码:

import networkx as nx 

def cost(i, j, d, value1, value2):
    # some operation involving all these values
    return cost


# graph is a networkx graph
# src, dst are integers
# cost is a callable that is passed 3 values automatically i.e. src, dst and d 
# d is a dictionary of edge weights
path = nx.dijkstra_path(graph, src, dst, weight=cost)
Run Code Online (Sandbox Code Playgroud)

现在我想通过两个值value1,并value2cost功能。

networkx文件表示,weight可以接受恰好3参数的调用。但我需要value1value2进行计算。如何才能做到这一点?

编辑 使用 functools …

python function callable

1
推荐指数
1
解决办法
4713
查看次数

标签 统计

python ×2

callable ×1

function ×1

numpy ×1