键值数据存储区,键是浮点值,值是对象

问题描述 投票:-1回答:1

我正在尝试制作一个简单的基于时间的脚本,用户在其中输入:

  1. 启动脚本后调用对象的时间,称为dt_call
    • [time.perf_counter()生成(又名float)]
  2. 当时要通话的对象

是否有一个Python库的键值存储满足以下条件?

  1. 键是float
  2. 值是object
  3. 键已排序

更多信息

这将是调度程序的一部分,调度程序经常在其中:

  1. 获取自启动脚本以来的当前时间(秒),称为dt
  2. 也许调用对象,取决于调用时间是否已过
    1. 看起来是if dT >= dt_call
    2. 如果是:检查相关对象是否已被调用。如果未调用,则调用该对象。
    3. 如果否:什么也不做

当前最佳想法

目前,我最好的主意是基于此:Sort a list of tuples by 2nd item (integer value)

启动脚本之前:

  1. dt_call +对象对存储在tuple中>
  2. 将所有对存储在列表中
  3. 使用此排序:https://stackoverflow.com/a/44852626/11163122
  4. # Keys are `dt_call`
    list_.sort(key=lambda x:x[0])
    
    list_  # [(5.6, obj0), (5.9, obj1), (8.7, obj2)]
    

启动脚本后:

  1. 使用index获取bisect.bisect
  2. 查看是否调用了index - 1处的对象。如果没有,请致电。
  3. # Start
    start_time = time.perf_counter()
    
    # Some time has passed
    dt = time.perf_counter() - start_time
    
    # Step 1
    index = bisect.bisect([x[0] for x in list_], dt)
    
    # Step 2
    fetched_obj = list_[index - 1][1]
    if fetched_obj.is_not_called():
        fetched_obj()
    

是否可以使用一种数据结构以更直接的方式(全部合而为一)?

这个想法结合了多种数据结构来完成工作。

我正在尝试制作一个简单的基于时间的脚本,用户在其中输入:启动脚本以调用对象后的时间,该对象称为dt_call由time.perf_counter()(又称它是浮点数)生成的对象...

python sorting data-structures key-value
1个回答
1
投票

您提到的需要一个允许以下内容的数据结构:

© www.soinside.com 2019 - 2024. All rights reserved.