>初始情况
假设我们有一个字典,以以下形式存储时间序列数据:
dic = {'M15':
{
'100001': { 0: [0,1,2,...],
1: [0,1,2,...]
},
'100002': { 0: [0,1,2,...],
1: [0,1,2,...]
},
...
},
'H1': {
'200001': { 0: [0,1,2,...],
1: [0,1,2,...]
},
...
},
...
}
现在,让我们假设这个字典存储在一个名为 data 的类中,如下所示:
class data:
def __init__(self, input: dict):
self.data = input
newData = data(dic)
显而易见,此类应存储时序数据,并在迭代中返回这些数据,以便在某个时间点进一步处理。
我的问题
我想使类可迭代,这意味着__next__
将遍历字典中的所有数据(即将到来的问题不是关于如何迭代嵌套字典,所以请不要回答这个问题)。数据意味着我只需要字典中最低级别的数组,例如[0,1,2,...]
.
让我们假设字典中的数据非常庞大 - 它可以放入内存,但不能重复。 因此,据我所知,列表理解不是一种选择,因为除了字典之外,数据也将存储在这个新列表中(仍然需要字典,并且在本例中不是一个选项数组)。 为了完整起见,这看起来像:
class data:
def __init__(self, input: dict):
self.dictionary = input
self.data = [series_array for series_key, series_array in series.items() for ... in self.dictionary.items()]
self.index = 0
def __iter__(self):
return self
def __next__(self):
self.index += 1
return self.data[self.index - 1]
问题1:
- 列表理解是否仅指向 字典还是真的会复制数据?
这意味着我将不得不对字典使用正常的迭代,但我想不出一种在__iter__
和__next__
中实现这一点的方法。
问题2:
- 我将如何在
__iter__
和__next__
中实现这个嵌套字典循环?
请注意,我正在寻找这个具体问题的答案,而不是"为什么不使用发电机"或"为什么不这样做/那样做"。
问题 1:
Would the list comprehension just point to the data within the dictionary or would it really copy the data?
它将保存对字典中列表的引用
问题2:
How would I implement this nested dictionary-loop within __iter__and __next__?
你只需要在__iter__
中返回一个迭代器(而不是例如列表),在这种情况下,列表中的生成器表达式就足够了:
class Data:
def __init__(self, input: dict):
self.dictionary = input
def __iter__(self):
return (series_array for series_key, series_array in series.items() for ... in self.dictionary.items())