【发布时间】:2015-07-30 16:42:30
【问题描述】:
我想从 numpy 数组 theoretical_price_for_bonds 中删除不满足特定条件的元素。我知道我可以使用下面的代码行来做到这一点。但是,我还想跟踪已删除元素的索引,我想知道如何才能做到这一点。
theoretical_price_for_bonds = theoretical_price_for_bonds[(theoretical_price_for_bonds>75)]
我尝试使用循环从 numpy 数组中动态删除元素。价格还可以,但dropped_indices 原来只是一个充满None 的列表:
#To insert values into a list dynamically
class GrowingList(list):
def __setitem__(self, index, value):
if index >= len(self):
self.extend([None]*(index + 1 - len(self)))
list.__setitem__(self, index, value)
count = 0
dropped_indices = GrowingList()
for x,value in np.ndenumerate(theoretical_price_for_bonds):
count = count + 1
if count < theoretical_price_for_bonds.shape[0]:
if theoretical_price_for_bonds[count] < 75:
theoretical_price_for_bonds = np.delete(theoretical_price_for_bonds, (count), axis=0)
dropped_indices[count] = count
谢谢
【问题讨论】: