【问题标题】:With a list of iterators, need to iterate through each completely before moving on to next one使用迭代器列表,需要完全迭代每个迭代器,然后再继续下一个
【发布时间】:2017-10-21 20:57:20
【问题描述】:

我有一个名为“last”的迭代器列表,每个迭代器包含 5 个值:

last = 
[<itertools.islice at 0x10bd29940>,
 <itertools.islice at 0x10cc914c8>,
 <itertools.islice at 0x10b94f7e0>]

每个系列索引(如下)也包含 5 个日期,我想在继续下一个之前完全遍历“最后一个”中的每个迭代器。 (对于每个 series.index ,遍历 5 次,然后当它移动到下一个 series.index 时,循环遍历下一个迭代器 5 次,等等。

到目前为止我已经尝试过;

for date in series.index:
    net = (next(last) - 100) / 100

for date in series.index:
    net = ([next(n) for n in last] - 100) / 100

不确定如何解决这个问题(itertools 中有什么东西吗?)

【问题讨论】:

  • 尝试使用itertools.chain.from_iterable链接迭代器
  • for it in last: for x in it...?
  • 你到底想在这里实现什么?什么是“下一个”系列索引 - 什么是系列索引?
  • [[x for x in it] for it in last] 创建一个列表,我需要一次取一个,例如 next(x) 用于 series.index 中的 ea 迭代
  • 不是让它成为一个 list-comp 而是让它成为一个 gen-exp?

标签: python iterator


【解决方案1】:

没有数据,预期的输出,很难猜到

但这里有一个尝试:(zip 对于迭代器上的“并行”操作很有用)

last = [range(1000,6000,1000), range(2000,7000,1000), range(3000,8000,1000)]

series =[range(1,6,1), range(2,7,1), range(3,8,1)]

[[*zip(srs, map(lambda x: (x - 100) / 100, ls))] for srs, ls in zip(series, last)]

[[(1, 9.0), (2, 19.0), (3, 29.0), (4, 39.0), (5, 49.0)],
 [(2, 19.0), (3, 29.0), (4, 39.0), (5, 49.0), (6, 59.0)],
 [(3, 29.0), (4, 39.0), (5, 49.0), (6, 59.0), (7, 69.0)]]

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2019-01-12
    • 1970-01-01
    • 2017-03-18
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多