我正在尝试找到列表中整数的索引。我有以下代码可以工作,但需要超过45秒。我是否可以使用更快的方法?我的代码如下:
for i in range(0,len(output)) :
indexes = [ii for ii,x in enumerate(Node1ID) if x == i].
我正在尝试找到列表中整数的索引。我有以下代码可以工作,但需要超过45秒。我是否可以使用更快的方法?我的代码如下:
for i in range(0,len(output)) :
indexes = [ii for ii,x in enumerate(Node1ID) if x == i].
Node1ID
列表 len(output)
次,每次增加您要查找的整数。您应该生成一个字典列表,其中您要查找的整数是索引,匹配的索引在相应的子列表中:indexes = {}
for i, x in enumerate(Node1ID):
indexes.setdefault(x, []).append(i)
这样你就可以用以下方式查找整数i
的匹配索引列表:
indexes.get(i, [])
# Get all the numbers to match (in this case len(output) = 10)
y = np.arange(10)
# Example array
x = [1,1,5,3,11]
y_indices, x_indices = np.where(x == y[:,None])
print(y_indices)
# array([1, 1, 3, 5])
print(x_indices)
# array([0, 1, 3, 2])
x[0] == 1
,x[1] == 1
,x[3] == 3
,x[2] == 5
。