[英]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.这将需要相当多的编码,所以我将写出应该可以帮助您完成工作的步骤。
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.