我正在使用Python的NetworkX图形库.在我的程序中的某个时刻,我想将我的nodeID"合并"成一系列数字.这是我的天真方法:
start = 1 # could be anything
for i, n in enumerate(g.nodes()):
if i+start == n:
continue
g.add_node(i+start, attr_dict=g.node[n])
g.add_edges_from([(i+start, v, g[n][v]) for v in g.neighbors(n)])
g.remove_node(n)
Run Code Online (Sandbox Code Playgroud)
有没有比这个所有邻居的详尽副本更快的方法?例如,我尝试过g[i+start] = g[n],但这是禁止的.
谢谢!
对JS来说是全新的,请原谅,如果这是令人难以置信的显而易见的话.
假设我想过滤一个字符串列表,其函数为f,映射字符串 - > bool.这有效:
filteredList = list.filter(function(x) { return f(x); })
Run Code Online (Sandbox Code Playgroud)
这失败了:
filteredList = list.filter(f)
Run Code Online (Sandbox Code Playgroud)
为什么???
代码示例:
~/projects/node (master)$ node
> var items = ["node.js", "file.txt"]
undefined
> var regex = new RegExp('\\.js$')
undefined
> items.filter(regex.test)
TypeError: Method RegExp.prototype.test called on incompatible receiver undefined
at test (native)
at Array.filter (native)
at repl:1:8
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
> items.filter(function(value) { return regex.test(value); } …Run Code Online (Sandbox Code Playgroud) 我的代码中有以下表达式:
a = (b / x[:, np.newaxis]).sum(axis=1)
Run Code Online (Sandbox Code Playgroud)
在哪里b是形状的ndarray (M, N),并且x是形状的ndarray (M,).现在,b实际上是稀疏的,所以对于内存效率我想用a scipy.sparse.csc_matrix或替换csr_matrix.然而,没有实现这种方式的广播(即使保证分割或乘法保持稀疏性)(条目x非零),并且提出a NotImplementedError.有sparse没有我不知道的功能会做我想做的事情?(dot()将沿错误的轴总和.)
我想,以产生zarr阵列指向部分磁盘上的zarr阵列,类似于如何sliced = np_arr[5]给我的视图成np_arr,使得在修改该数据sliced修改的数据np_arr。示例代码:
import matplotlib.pyplot as plt
import numpy as np
import zarr
arr = zarr.open(
'temp.zarr',
mode='a',
shape=(4, 32, 32),
chunks=(1, 16, 16),
dtype=np.float32,
)
arr[:] = np.random.random((4, 32, 32))
fig, ax = plt.subplots(1, 2)
arr[2, ...] = 0 # works fine, "wipes" slice 2
ax[0].imshow(arr[2]) # all 0s
arr_slice = arr[1] # returns a NumPy array — loses ties to zarr on disk
arr_slice[:] = 0
ax[1].imshow(arr[1]) # …Run Code Online (Sandbox Code Playgroud)