如何在新迭代器中生成迭代器中的元素?

How to yield the elements in an iterator within a new iterator?

我需要一个遍历 JSON 对象中所有叶子的迭代器。所以我写了这个函数

rec = {'a': {'b': [{'c': {'d': [{'e': 'x1','f': 'x2'}],'g': 'x3'}}],'h': 'x4','i': 'x5','j': [{'k': 'x6'}],'l': [{'m': {'n': 'x7'}}]}}

def yield_leaves(rec, lbl = ''):
  if isinstance(rec, dict):
    for key, value in rec.items():
        for to_yield in yield_leaves(value, key):
            yield to_yield
  if isinstance(rec, list):
    for value in rec:
        for to_yield in yield_leaves(value, lbl):
            yield to_yield
  if isinstance(rec, (int, str)):
    for entry in rec.split():
        yield entry, lbl

print(list(yield_leaves(rec)))
>>> [('x5', 'i'), ('x4', 'h'), ('x1', 'e'), ('x2', 'f'), ('x3', 'g'), ('x6', 'k'), ('x7', 'n')]

但我认为有些代码是多余的。下面一行

for to_yield in yield_leaves(value, key):
        yield to_yield

它遍历一个迭代器,returns 值作为迭代器的一部分。

你知道更有效的编码方式吗?

尝试使用 yield from 而不是 for

yeild from yield_leaves(value, key)

P.S。如果您使用 python >3.3

你的最后一节

if isinstance(rec, (int, str)):
    for entry in rec.split():
        yield entry, lbl

有点奇怪。如果 recint,它将崩溃,因为整数没有 .split 方法。 none 的字符串包含空格,因此对它们调用 .split 只会 return 一个包含单个项目的列表:原始字符串。我猜你的真实数据可能包含你想要拆分的多词字符串,但如果是这样,你真的需要与 ints.

分开处理

因此,假设您没有想要拆分的多词值,我已经稍微简化了您的代码。如您所见,我只保留了 dictlist 测试,因为数据是从 JSON 解码而来的,任何其他 rec 类型都将是某种标量:intstrboolNone(除非您创建了自定义解码),我们可以相同地处理所有这些标量类型。

rec = {
    'a': {
        'b': [
            {
                'c': {
                    'd': [{'e': 'x1', 'f': 'x2'}],
                    'g': 'x3'
                }
            }
        ],
        'h': 'x4',
        'i': 'x5',
        'j': [{'k': 'x6'}],
        'l': [{'m': {'n': 'x7'}}]
    }
}

def yield_leaves(rec, lbl=''):
    if isinstance(rec, dict):
        for key, value in rec.items():
            yield from yield_leaves(value, key)
    elif isinstance(rec, list):
        for value in rec:
            yield from yield_leaves(value, lbl)
    else:
        yield rec, lbl

print(list(yield_leaves(rec)))

输出

[('x1', 'e'), ('x2', 'f'), ('x3', 'g'), ('x4', 'h'), ('x5', 'i'), ('x6', 'k'), ('x7', 'n')]

此代码使用了 Python 3 功能 yield from;如果您不使用 Python 3,您应该使用。 :)