简体   繁体   English

如何使用来自另一个字典的聚合值创建一个新字典

[英]How to create a new dictionary using aggregate values from another dictionary

I have a dictionary with time of day as the key and stock price as the value.我有一本字典,以一天中的时间为键,以股票价格为值。 There is a key for each minute in a day:一天中的每一分钟都有一个键:

original_dict = {'09:30': '2170.8601', '09:31': '2180.21', '09:32': '2181.76'... '16:15': '2179.98'}

I am trying to create a new dictionary which will have times as keys and values to be a list consisting of open, high, low and close stock prices (values from the original dictionary) using rolling 10 minute increments:我正在尝试创建一个新的字典,它将把时间作为键和值作为一个列表,其中包含开盘价、最高价、最低价和收盘价(来自原始字典的值),使用滚动 10 分钟增量:

new_dict = {'09:40': [open, high, low, close], '09:41': [open, high, low, close]...}

( new_dict starting at 9:40 because 10 minutes is required for the first key/list to be calculated) new_dict从 9:40 开始,因为计算第一个键/列表需要 10 分钟)

For example, the list of values for the 9:40 key in the new_dict should be calculated using the values of 9:30-9:39 in the original_dict .例如,对于在所述9:40密钥值的列表new_dict 30-9:在39应该使用的9的值来计算original_dict The open, high, low and close are the 9:30 , max of 9:30-9:39 , min of 9:30-9:39 , and 9:39 values from original_dict , respectively.开放,高,低和关闭是9:30 ,最大的9:30-9:39的,分9:30-9:39 ,和9:39从值original_dict ,分别。 The 9:41 key will have open, high, low, close values calculated using 9:31-9:40 values from original_dict and so on. 9:41键将具有使用来自original_dict 9:31-9:40值计算的开盘价、最高9:41 、最低价、收盘9:31-9:40等。

I am not sure how to easily do a for statement, for example, using original_dict since it is not a list.我不确定如何轻松执行 for 语句,例如,使用original_dict因为它不是列表。 Should I create an intermediate list of keys from original_dict and use that to iterate through times?我应该从original_dict创建一个中间键列表并使用它来迭代时间吗?

What would be the best way to create new_dict ?创建new_dict的最佳方法是new_dict

Thank you!谢谢!

It would require a fair bit of coding so I'm going to instead write out steps that should help you get the job done.这将需要相当多的编码,所以我将写出应该可以帮助您完成工作的步骤。

  1. Convert the String keys ('09:30') to numbers('09:30' is 9*60 + 30 = 2130 hours).将字符串键 ('09:30') 转换为数字 ('09:30' 是 9*60 + 30 = 2130 小时)。
  2. Iterate from the earliest minute in your dictionary to the last minute, and store the values associated with each minute in a list.从字典中最早的一分钟到最后一分钟进行迭代,并将与每一分钟关联的值存储在一个列表中。 You should have a list of values sorted chronologically now.您现在应该有一个按时间顺序排序的值列表。
  3. Maintain a sliding window maximum queue and sliding window minimum queue, with the window size in your case being 10.维护滑动窗口最大队列和滑动窗口最小队列,您的情况下的窗口大小为 10。
  4. Then at every step, fetch the 'high price' from the sliding window maximum queue, and likewise for the 'low price'.然后在每一步,从滑动窗口最大队列中获取“高价”,“低价”也是如此。 For the 'start price', you just need to look back 10 indexes behind in the sorted list of values we earlier formed.对于“起始价格”,您只需回顾我们之前形成的排序值列表中的 10 个索引。 For the 'end price', we look behind just a single index.对于“最终价格”,我们只关注单一指数。

And that should be it.应该就是这样。 Perhaps the tricky part here would be implementing the Sliding Window Maximum/Minimum, you could take the following implementation of a function that takes in a list of numbers, and returns another list of of the maximum of every contiguous window of size k in our list.也许这里棘手的部分是实现滑动窗口最大值/最小值,您可以采用以下函数的实现,该函数接受一个数字列表,并返回我们列表中每个大小为 k 的连续窗口的最大值的另一个列表. Runs in O(N) time where N is the size of the list.在 O(N) 时间内运行,其中 N 是列表的大小。

def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
    deque = collections.deque()
    res = []
    for i, num in enumerate(nums):
        while(deque and nums[deque[-1]] < num):
            deque.pop()
        if(deque and i - deque[0] >= k):
            deque.popleft()
        deque.append(i)
        res.append(nums[deque[0]])
    return res[k-1:]

You'd similarly obtain Sliding Window Minimum, and with that you should have hopefully enough to get it working.您同样会获得滑动窗口最小值,并且您应该有足够的希望让它工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个字典的特定值创建新的 Python 字典 - Create new Python dictionary from specific values from another dictionary 使用for循环使用其他词典中的键创建新词典 - Use a for loop to create a new dictionary using keys from another dictionary 用另一个字典的值替换一个字典的键以创建一个新的字典 - Replacing the keys of a dictionary with values of another dictionary to create a new dictionary 如何根据另一个字典中的奇数和偶数值创建字典? - How to create a dictionary based on odd and even values from another dictionary? 如何使用 for 循环从具有相同键但不同值的旧字典创建新字典 - How to create a new dictionary from an old dictionary with the same keys but different values using a for loop 从一个字典的键和另一个字典的值构建一个新字典 - Build a new dictionary from the keys of one dictionary and the values of another dictionary 如何使用以前词典中的键和值创建词典? - How to Create a Dictionary Using Keys and Values from a Previous Dictionary? 如何通过修改特定值从另一个字典创建新字典(将两个键的值合二为一) - How to create a new dictionary from another one by modifying specific values (join the values of two keys into one) 如何从另一个字典值制作字典 - How to make a dictionary from another dictionary values Python - 根据给定的键数动态创建新字典中的键数:值:另一个字典中的值 - Python - dynamically create number of key:values in a new dictionary, based on given number of key:values from another dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM