我有一个过滤器表达式如下:
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代码:
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,并value2给cost功能。
该networkx文件表示,weight可以接受恰好3参数的调用。但我需要value1和value2进行计算。如何才能做到这一点?
编辑 使用 functools …