Iterator-class 用于嵌套字典
Iterator-class for nested dictionaries
初始情况
假设我们有一个字典以以下形式存储时间序列数据:
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,...]
},
...
},
...
}
现在,让我们假设这个字典存储在一个 class 的数据中,如下所示:
class data:
def __init__(self, input: dict):
self.data = input
newData = data(dic)
显而易见,此 class 应存储时间序列数据,并 return 将其存储在迭代中,以便在某个时间点进行进一步处理。
我的问题
我想让 class 可迭代,意思是 __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]
问题一:
- 列表理解是否仅指向
字典还是真的会复制数据?
这意味着我必须对字典使用正常的迭代,但我想不出在 __iter__
和 __next__
.
中实现它的方法
问题二:
- 我如何在
__iter__
和 __next__
中实现这个嵌套的字典循环?
请注意,我正在寻求这个具体问题的答案,而不是 "why don't use generators" 或 "why don't do it this/that way"。
Question 1:
Would the list comprehension just point to the data within the dictionary or would it really copy the data?
它将包含对字典中列表的引用
Question 2:
How would I implement this nested dictionary-loop within __iter__and __next__?
您只需要 return __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())
初始情况
假设我们有一个字典以以下形式存储时间序列数据:
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,...]
},
...
},
...
}
现在,让我们假设这个字典存储在一个 class 的数据中,如下所示:
class data:
def __init__(self, input: dict):
self.data = input
newData = data(dic)
显而易见,此 class 应存储时间序列数据,并 return 将其存储在迭代中,以便在某个时间点进行进一步处理。
我的问题
我想让 class 可迭代,意思是 __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]
问题一:
- 列表理解是否仅指向 字典还是真的会复制数据?
这意味着我必须对字典使用正常的迭代,但我想不出在 __iter__
和 __next__
.
问题二:
- 我如何在
__iter__
和__next__
中实现这个嵌套的字典循环?
请注意,我正在寻求这个具体问题的答案,而不是 "why don't use generators" 或 "why don't do it this/that way"。
Question 1:
Would the list comprehension just point to the data within the dictionary or would it really copy the data?
它将包含对字典中列表的引用
Question 2:
How would I implement this nested dictionary-loop within __iter__and __next__?
您只需要 return __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())