python - 有没有办法让一种方法在另一种方法中从生成器获取下一个值?

标签 python pandas generator

我的 get_new_point 方法中有一个生成器,它从字典中的数据框中检索数据元组。理想情况下,当我运行 update_trailing_points 方法时,它会从生成器中检索下一个元组。但是,当我实际运行 update_trailing_points 方法时,它每次都返回相同的元组(数据帧的第一行)。我在这里做错了什么?

让我解释一下我的数据结构,因为我知道它们可能有点困惑:

all_data 是数据帧的字典。键是“符号”

symbol_list 是键(符号)的列表

    def get_new_point(self,symbol):
        for i in self.all_data[symbol].index:
            yield tuple([self.all_data[symbol]['timestamp'][i],\ 
                         self.all_data[symbol]['point'][i])

    def update_trailing_points(self):
        for s in self.symbol_list:
            try:
                new_point = self.get_new_point(s).__next__()
            except StopIteration:
                self.continue_test = False

最佳答案

self.get_new_point(s) 返回生成器对象。

new_point = self.get_new_point(s).__next__()

每次运行这段代码时,您都在重新初始化生成器。 您应该只调用一次 self.get_new_point(s),但多次调用该对象的 __next__()

关于python - 有没有办法让一种方法在另一种方法中从生成器获取下一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56068622/

相关文章:

python - numpy 计算滞后于领导者时间戳和滞后时间戳之间

python - 派斯帕克 2 : KMeans The input data is not directly cached

python - groupby 在有序分类列上的奇怪行为

generics - Swift 2.0 版本的 struct GeneratorOf<T>

git - 如何从 git 存储库创建信息网站

python - 使用 pip 在 vi​​rtualenv 中更新 matplotlib

python - 是否可以将不同长度的列表作为空数据框中的列附加?

python - 如何在单个 np.where 条件中使用多个值?

python - "by = lambda x: lambda y: getattr(y, x)"是什么意思?

python - 装饰迭代器以在 python 中调用 next 之前更改值的好方法是什么?