我从订单簿中获取值,如下所示:
list1 = [...,'ethbtc', '0.077666', '10', '0.077680', '15',...]
------------------------^符号-----^价值---^数量--
这个列表中大约有100个符号,每个符号有40个值。它们总是处于同一顺序。
我想知道,如果我支付余额的100%,我的系统在这一刻购买的最高价格是多少。在
因此,如果我想以0.077666的价格购买11以太币,那么实际价格应该是0.077680,因为第一个价格只有10个以太币。
我不想得到平均值,因为那将是目前太多
我的代码有一个嵌套的for循环,并循环两个列表:
symbollist = [ethbtc, eoseth,...]
a
的索引列表,因为值和数量总是在同一位置a = ['1', '3', '5', ...]
我的代码:
for symbolnow in symbollist:
sumlist = []
for i in a:
quantity = float(list1[list1.index(symbolnow) + (i+1)] if symbolnow in list1 else 0)
sumlist.append(quantity)
if sum(sumlist) > mycurrentbalance:
maxvalue = float(list1[list1.index(symbolnow) + i] if symbolnow in list1 else -1)
break
else:
maxvalue = -1
那么这段代码是做什么的:
1) 循环遍历符号列表中的每个符号
2) 对于找到的每个符号,我都会查找可用数量
3) 如果我的余额(即10 ETH)小于qty,则循环中断
4) 如果没有,则继续搜索和汇总汇总列表中的每个数量,直到有足够的数量为止。在
代码按预期工作,但没有那么快。正如预期的那样,list1.index
需要很长时间才能执行。。在
问题
一个更快的代码是如何工作的。在这种情况下,列表理解更好吗?甚至是正则表达式?我的代码很难看吗?在
提前谢谢你!在
编辑:
为了澄清输入和期望输出,示例:
list1 = [...,'ethbtc', '0.077666', '1', '0.077680', '1.5', '0.077710', '3', '0.078200', '4',...]
mycurrentbalance = 5.5
<;--余额以ETH为单位list1
中每三个条目都是以太币的数量,因此在列表中它将是['1', '1.5', '3', '4']
所以如果我想卖掉我所有的ETH(在这个例子中是5.5),最大值应该是'0.077710'
list1
包含100个符号,因此在'ethbtc'
之前和之后还有其他值、数量和符号
除了用户3080953给出的答案外,您还必须对数据进行预处理,这不仅是因为这样会更有效,而且还因为它将帮助您处理复杂性。在这里,您同时做两件事:解码列表和使用数据。先解码,再使用。在
我认为,目标格式应该是:
现在,你只需:
^{pr2}$如何获取目标格式的数据?只需迭代列表,如果找到符号,则开始存储值和数量,直到下一个符号:
您有一个轻微但有趣的优化,特别是如果您的数量/值列表很长。不要存储数量,而是存储总数量:
现在,您可以使用
bisect
快速找到您的max_值。代码变得更容易理解,因为bisect.bisect_left(rts, my_current_balance)
将返回第一次运行的总计>= my_current_balance
的索引:要建立运行总数,您必须以不同的方式处理价格和数量:
将所有内容放入函数中(或者更好地说,类的方法):
你可以看到,通过将问题分成两部分(解码和使用),代码变得更快(在我看来)更容易理解(我没有发表评论,但它们应该在那里)。在
预处理
list1
并将其存储在dict中。这意味着您只需在list1
上迭代一次,而不是每次内部循环运行时。在不要迭代}(python2)。这将使用迭代器而不是列表,从而使代码更加灵活。在
^{pr2}$a
,而是迭代range
(python3)或{在您的例子中,如果有固定的间隔,我认为使用slice对象将有助于您的“a”循环。您可以将列表切片保存到一个对象,如下所示(还有1个或2个其他提示)。我同意上面的用户,如果你有机会预处理输入的数据,那么你真的必须。为此,我建议使用pandas库,因为它非常快,但是字典也允许散列值。在
相关问题 更多 >
编程相关推荐