Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数组中的值出现在另一个数组中的索引_Python_Networkx - Fatal编程技术网

Python 数组中的值出现在另一个数组中的索引

Python 数组中的值出现在另一个数组中的索引,python,networkx,Python,Networkx,我有这个阵列: n=nodes of the graph a=np.random.choice(n,size=3) prob=np.zeros((1,n)) 如何根据节点的索引将a中的节点分配给prob? 我有以下代码: for k in a: m=nodes.index(k) b=np.zeros((1,n)) b[0][m]=1 here the results [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0

我有这个阵列:

n=nodes of the graph
a=np.random.choice(n,size=3)
prob=np.zeros((1,n))
如何根据节点的索引将
a
中的节点分配给
prob
? 我有以下代码:

for k in a:
    m=nodes.index(k)
    b=np.zeros((1,n))
    b[0][m]=1
here the results

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
但对我来说,我想要这个结果

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

如果您在每次迭代中重置
b
,您需要在
for
循环之前初始化
b
,然后使用
m
的结果更新每个循环中的
b
,我们将不胜感激。还要注意,
b
只需要一个轴。让我们考虑下面的节点列表,以及一个数组<代码> A<代码>节点,用来查找<代码>节点< /代码>:

nodes = [5,12,6,1,3,9,4,8,2,45]
a = np.array([5, 9, 8, 4])
调整您的方法,您将拥有:

b=np.zeros(len(nodes))
for k in a:
    m=nodes.index(k)
    b[m]=1

print(b)
# array([1., 0., 0., 0., 0., 1., 1., 1., 0., 0.])
但是请注意,这具有不必要的
O(len(节点)*len(a))
复杂性。更好的方法是构建一个查找表,将值映射到
节点中的位置,并将
b
分配为:

d = {v:k for k,v in enumerate(nodes)}

b=np.zeros(len(nodes))
for k in a:
    b[d[k]] = 1

print(b)
#array([1., 0., 0., 0., 0., 1., 1., 1., 0., 0.])

另外,由于您使用的是numpy,我们可以使用
np.searchsorted
np.bincount
来加速上述操作:

nodes = np.array(nodes)
nodes_s = nodes.argsort()
s = np.searchsorted(nodes[nodes_s],a)
b = np.bincount(nodes_s[s], minlength=len(nodes))

print(b)
# array([1, 0, 0, 0, 0, 1, 1, 1, 0, 0])

在每次迭代中重置
b
,需要在
for
循环之前初始化
b
,然后在每次循环中更新
b
,结果为
m
。还要注意,
b
只需要一个轴。让我们考虑下面的节点列表,以及一个数组<代码> A<代码>节点,用来查找<代码>节点< /代码>:

nodes = [5,12,6,1,3,9,4,8,2,45]
a = np.array([5, 9, 8, 4])
调整您的方法,您将拥有:

b=np.zeros(len(nodes))
for k in a:
    m=nodes.index(k)
    b[m]=1

print(b)
# array([1., 0., 0., 0., 0., 1., 1., 1., 0., 0.])
但是请注意,这具有不必要的
O(len(节点)*len(a))
复杂性。更好的方法是构建一个查找表,将值映射到
节点中的位置,并将
b
分配为:

d = {v:k for k,v in enumerate(nodes)}

b=np.zeros(len(nodes))
for k in a:
    b[d[k]] = 1

print(b)
#array([1., 0., 0., 0., 0., 1., 1., 1., 0., 0.])

另外,由于您使用的是numpy,我们可以使用
np.searchsorted
np.bincount
来加速上述操作:

nodes = np.array(nodes)
nodes_s = nodes.argsort()
s = np.searchsorted(nodes[nodes_s],a)
b = np.bincount(nodes_s[s], minlength=len(nodes))

print(b)
# array([1, 0, 0, 0, 0, 1, 1, 1, 0, 0])

Wao,wanderfull answe,非常有用,谢谢你很高兴它帮助了@christinamuro:)如果它为你解决了问题,别忘了接受。看,谢谢!Wao,wanderfull answe,非常有用,谢谢你很高兴它帮助了@christinamuro:)如果它为你解决了问题,别忘了接受。看,谢谢!