如何将其转换为单线发生器?

时间:2010-12-02 16:49:19

标签: python

我最好的尝试失败了:

for s in self.services:
  for m in s.messages: yield m

2 个答案:

答案 0 :(得分:7)

(m for s in self.services for m in s.messages)

......看起来像反直觉一样。

答案 1 :(得分:3)

itertools.chain.from_iterable(s.messages for s in self.services)