【问题标题】:How can I iterate this code so that I can save the value of last traded in the list dynamically?如何迭代此代码以便我可以动态保存列表中最后交易的值?
【发布时间】:2019-10-28 09:26:12
【问题描述】:

如何迭代此代码,以便动态保存列表中上次交易的值?

def get_ltp(data, strike_price):
    for d in data:
        if strike_price == d[5]:
            return d[5]
    n=get_ltp(data, '12000.00')
    n2=int(n.replace(',', ''))
    vol_opt.append(n2)
    print(vol_opt)
threading.Timer(5, get_ltp(data,'12000.00')).start()

get_ltp(data, '12000.00')

【问题讨论】:

  • 请正确格式化代码。
  • @ Tech Nerd 代码现在采用所需格式。
  • 你想让代码做什么?它有什么作用?
  • 代码应该每5秒迭代一次,vol_opt是一个列表,其中包含'last price'的值,每5秒代码应该提供列表中的元素。
  • 那么代码基本上应该每5秒运行一次?

标签: python json beautifulsoup


【解决方案1】:

由于您只需要每 5 秒运行一个线程,因此您可以使用带有 time.sleep()While True: 子句来休眠 x 秒。

import time

def get_ltp(data, strike_price):
    for d in data:
        if strike_price == d[5]:
            return d[5]

while True:
    time.sleep(5)
    get_ltp(data, '12000.00')


【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 2018-11-22
    • 2021-07-25
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    相关资源
    最近更新 更多