我有一个类似于下面的numpy数组,代表每个人的邻居(这首先由igraph包生成然后转换为numpy数组
import numpy as np
import igraph
Edges = 2
NumNodes = 30
DisGraph = igraph.GraphBase.Barabasi(NumNodes, Edges)
Neighbors = map(DisGraph.neighbors, range(NumNodes))
Neighbors = np.asarray(DisNeighbors)
):
Neighbors=[[1, 2, 3, 4, 5, 6, 8, 9, 11, 23, 24, 27]
[0, 2, 3, 4, 9, 10, 13, 16, 17, 19, 25, 27] [0, 1, 10, 22]
[0, 1, 5, 6, 7, 8, 12, 14, 15, 21, 22] [0, 1]
[0, 3, 7, 11, 15, 23, 24, 25, 29] [0, 3] [3, 5, 18] [0, 3, 12, 16, 18]
[0, 1, 13] [1, 2, 14, 20] [0, 5] [3, 8, 19] [1, 9, 21, 28]
[3, 10, 17, 20, 26] [3, 5] [1, 8] [1, 14, 26] [7, 8] [1, 12] [10, 14, 28]
[3, 13] [2, 3] [0, 5] [0, 5] [1, 5] [14, 17] [0, 1, 29] [13, 20] [5, 27]]
我想找到一种方法从这个阵列中获取某些数字, 可能没有使用循环。 例如,如果我有一个列表:
List = [0 1 2 3 4 5 6 7 8 9 10]
然后,我希望生成的Neighbors数组在List中删除这些值。
任何帮助将不胜感激。
我目前的答案如下:
for aa in List:
i=0
for bb in Neighbors:
Neighbors[i] = [cc for cc in bb if cc != aa]
i=i+1
但是我想知道是否有更有效的方法来处理这个问题,因为我正在处理数百万的数组。
答案 0 :(得分:1)
我不知道你的意思"将这些值移到列表中" (你是什么意思,"删除"?)。但是,通常,您可以通过以下方式选择数组中的点:
import numpy as np
a = np.random.random_integers(0,10,[10,10])
b = np.random.random_integers(0,10,5)
for r in b:
a[a==r] = -999
a
Out[12]:
array([[ 5, 1, -999, 3, 7, 5, 8, 3, 8, 4],
[ 8, 8, -999, 7, -999, 8, -999, -999, 4, 7],
[ 10, -999, -999, -999, -999, 1, -999, 7, 10, -999],
[ 3, 10, 8, -999, 8, 4, -999, 7, 4, 3],
[ 4, -999, 4, -999, -999, -999, -999, -999, -999, -999],
[ 5, 3, -999, 10, 10, -999, 10, 10, 3, 8],
[ 8, 5, -999, -999, 7, -999, 1, 8, -999, 8],
[ 4, 3, 8, -999, 3, 5, 4, -999, 4, 10],
[ 4, 3, 7, 4, -999, 7, 7, 7, -999, 8],
[-999, 10, -999, 5, 1, 5, 1, 10, 5, 1]])