Python 如何编写累加器以围绕中点挤压数组?
假设我有一个数组,如下所示:Python 如何编写累加器以围绕中点挤压数组?,python,numpy,Python,Numpy,假设我有一个数组,如下所示: array([[ 7108.4, -600. ], [ 7101.3, -500. ], [ 7094.3, -400. ], [ 7087.2, -300. ], [ 7080.1, -200. ], [ 7073.1, -100. ], [ 7058.3, 100. ], [ 7051.3, 200. ], [ 7044.2,
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
这是一个价格和数量列表,正数表示买入,负数表示卖出
如果当前市场价格为7050,我想“挤压”我的价格,如下所示:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7050. , 300. ], # Price & quantity changed here
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
从另一个方向做同样的事情,所以如果市场价格是7085:
array([[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7085. , -300. ], # Price & quantity change here
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]])
写这种东西最干净的方式是什么?理想情况下,我希望将挤压和数量分组作为两个单独的步骤来编写。这是一种操作,可以通过以下方式简化
由于Eric的回答很好,但根据您的评论: 可以修改原始数组,以便输出为 内部有变更的完整订单列表 以下是我不带numpy的方法:
array=[[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]]
price=0
positive_value=7050
updated_list=[]
track=[]
for index,item in enumerate(array):
if item[0]>positive_value:
if item[1]>0:
price+=item[1]
track.append(index)
else:
updated_list.append(item)
else:
updated_list.append(item)
updated_list.insert(track[0],[positive_value,price])
print(updated_list)
输出:
[[7108.4, -600.0], [7101.3, -500.0], [7094.3, -400.0], [7087.2, -300.0], [7080.1, -200.0], [7073.1, -100.0], [7050, 300.0], [7044.2, 300.0], [7037.1, 400.0], [7030.1, 500.0], [7023.0, 600.0]]
[[7108.4, -600.0], [7101.3, -500.0], [7094.3, -400.0], [7087.2, -300.0], [7085, -300.0], [7058.3, 100.0], [7051.3, 200.0], [7044.2, 300.0], [7037.1, 400.0], [7030.1, 500.0], [7023.0, 600.0]]
第二个:
array=[[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]]
price=0
positive_value=7085
updated_list=[]
track=[]
for index,item in enumerate(array):
if item[0] < positive_value:
if item[1] < 0:
price+=item[1]
track.append(index)
else:
updated_list.append(item)
else:
updated_list.append(item)
updated_list.insert(track[0],[positive_value,price])
print(updated_list)
逻辑是什么?当你指定7050时,为什么只有7051.3和7058.3?因为这些价格高于市场价格,我们不想提供高于市场价格的价格。那么7073.1、7080.1……呢?在这种情况下,市场价格是7085,所以我们不想提供低于市场价格的价格。你可以看出我们在销售,因为数量是负数。为什么数据中有12行,结果中有12+1-2?这是个好主意。我可以在这里做一个赋值,然后将其返回到原始np格式吗?@cjm2671:这主要是为了方便和命名列。如果你想留在
np
,你也可以。所以实际上你用numpy做的是我最初的实现,但是我不能在最初的数组上做最后的赋值。是否可以修改原始数组,以便输出的是内部有更改的完整订单列表?
array=[[ 7108.4, -600. ],
[ 7101.3, -500. ],
[ 7094.3, -400. ],
[ 7087.2, -300. ],
[ 7080.1, -200. ],
[ 7073.1, -100. ],
[ 7058.3, 100. ],
[ 7051.3, 200. ],
[ 7044.2, 300. ],
[ 7037.1, 400. ],
[ 7030.1, 500. ],
[ 7023. , 600. ]]
price=0
positive_value=7085
updated_list=[]
track=[]
for index,item in enumerate(array):
if item[0] < positive_value:
if item[1] < 0:
price+=item[1]
track.append(index)
else:
updated_list.append(item)
else:
updated_list.append(item)
updated_list.insert(track[0],[positive_value,price])
print(updated_list)
[[7108.4, -600.0], [7101.3, -500.0], [7094.3, -400.0], [7087.2, -300.0], [7085, -300.0], [7058.3, 100.0], [7051.3, 200.0], [7044.2, 300.0], [7037.1, 400.0], [7030.1, 500.0], [7023.0, 600.0]]