我有一个试图在循环中访问的子图列表:
index=[5,3,4,1,1,3,4,2,3,4,2,2,3,3,2,4]
subgraph=[[subgraph1],[subgraph2],[subgraph3],[subgraph4],[subgraph5]]
for i in range(len(index)):
for j in range(i+1,len(index)):
if index[j]==index[i]
continue
testgraphi=copy.copy(subgraph[index[i]])
testgraphj=copy.copy(subgraph[index[j]])
因此,在第一个循环中,testgraphi
将被指定为subgraph5
,而testgraphj
将被分配为subgraph3
。但是,当我尝试此方法时,将返回一个错误list indices must be integers, not numpy.float64
。这是合乎逻辑的,因为index
实际上是在我的程序的整个过程中作为一个numpy数组初始化的(而且它必须保持这种方式)。因此,我的问题是,如何将此值转换为可以用作我的子图列表的索引?这样,在初始化testgraph
时,它将检索循环所在的索引的值,并使用它来定义返回哪个列表索引?
发布于 2015-06-26 21:30:59
你可以试试这个:
new_index = np.asarray(index, dtype=np.int32)
这应该将索引中的所有值转换为整数。
发布于 2015-06-26 21:34:21
您可以通过执行以下操作将numpy.float64
转换为浮动:var.item()
。然后将其转换为整数,以便将其用作索引:int(var.item())
https://stackoverflow.com/questions/31082291
复制相似问题