如何在python中不断更新固定长度的列表? [英] How to constantly update a list of fixed length in python?

查看:877
本文介绍了如何在python中不断更新固定长度的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉出现菜鸟问题.我才刚刚开始编写代码,所以我需要跟踪1小时的价格历史记录.

Sorry for the noob question. I'm just starting to code and I need to keep track of the 1hr price history.

我想每秒将值拉入大小为3600的列表中,直到列表被填满,然后每秒从此列表向左移动一次,以便价格保持不变.

I wanted pull values every second into a list of size 3600 until the list is filled, then shift the list to the left every second from there on out so the prices would stay constant.

while True:
  polo = exchange.returnTicker()
  ethBtcRatio = polo["BTC_ETH"]['last']
  priceHistory = []
  ## What do I do here? Append?
  time.sleep(1)

有什么想法吗?

推荐答案

您可以附加每个新条目,然后检查列表的长度并有条件地priceHistory.pop(0).

You could append each new entry, then check the length of the list and conditionally priceHistory.pop(0).

看起来好像您正在循环中初始化priceHistory列表.您希望在循环之前执行此操作,因此不必每次都将空列表分配给priceHistory.

Looks like you're initializing the priceHistory list in the loop though. You want to do that before the loop, so you don't end up assigning empty lists to priceHistory every time.

或者,这取决于用例,那就是您可以创建一个包含3600个None条目的初始列表,然后每次都添加并弹出而无需进行条件长度检查.

Alternatively, and this depends on the use case, is that you can create an initial list with 3600 None entries, then just append and pop every time without the conditional length check.

这篇关于如何在python中不断更新固定长度的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆