gpt4 book ai didi

python - 在 Python 中将文本与嵌套的 OrderedDict 分开

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:15 24 4
gpt4 key购买 nike

这是我的 OrderedDict 对象,

a=OrderedDict([(u'p', [u'"The Exam Room" is a new series in which everyday medical questions are answered by physicians and professors from the Yale School of Medicine.', u'In our second episode: Dr. Stephen Strittmatter, Vincent Coates Professor of Neurology and director of the Adler Memory Clinic in Neurology, explains when memory loss can become a problem and what you can do to boost your brain power.', OrderedDict([(u'em', u'Produced & Hosted by Noah Golden')])])])

我想做的是从这个对象中获取文本,

>>> a.get('p')

并得到输出,

[u'"The Exam Room" is a new series in which everyday medical questions are answered by physicians and professors from the Yale School of Medicine.', u'In our second episode: Dr. Stephen Strittmatter, Vincent Coates Professor of Neurology and director of the Adler Memory Clinic in Neurology, explains when memory loss can become a problem and what you can do to boost your brain power.', OrderedDict([(u'em', u'Produced & Hosted by Noah Golden')])]

但生成的文本还包含一个 OrderedDict

我如何合并来自 OrderedDict ,

的文本

预期输出:

The Exam Room" is a new series in which everyday medical questions are answered by physicians and professors from the Yale School of Medicine.', u'In our second episode: Dr. Stephen Strittmatter, Vincent Coates Professor of Neurology and director of the Adler Memory Clinic in Neurology, explains when memory loss can become a problem and what you can do to boost your brain power. Produced & Hosted by Noah Golden

最佳答案

如果您事先不知道类型的嵌套,这里的关键是递归。这是一个示例(为了便于阅读而对文本进行了格式化):

#!/usr/bin/env python

import collections

a = collections.OrderedDict([(u'p', [u"""
"The Exam Room" is a new series in
which everyday medical questions are answered by physicians and
professors from the Yale School of Medicine.""",
u"""In our second episode: Dr. Stephen Strittmatter,
Vincent Coates Professor of Neurology and director of
the Adler Memory Clinic in Neurology, explains when
memory loss can become a problem and what you can do to
boost your brain power.""",
collections.OrderedDict([(u'em',
u'Produced & Hosted by Noah Golden')])])])

现在展平对象,它可能是映射或列表。实现了三个选项:如果找到的值是一个字符串,我们只需将它附加到我们的 collector。如果它是一个 list 或一个 Mapping,我们再次调用 flatten。请注意,您可以使用 allowed kwarg 指定一些允许的标签:

def flatten(obj, allowed=(u'p', u'em')):
collector = []

def process(v, collector=collector):
if isinstance(v, (list, collections.Mapping)):
collector += flatten(v, allowed=allowed)
elif isinstance(v, basestring):
collector.append(v)
else:
raise ValueError('Cannot handle type: {t}'.format(t=v.__class__))

if isinstance(obj, list):
for v in obj:
process(v)

if isinstance(obj, collections.Mapping):
for k, v in obj.iteritems():
if k in allowed:
process(v)

return collector

if __name__ == '__main__':
print(flatten(a))

您的示例的结果将是一个三元素列表,看起来像这样:

[u'"The Exam Room" is a new series ...',
u'In our second episode: ...',
u'Produced & Hosted by Noah Golden']

现在,如果您想要单个字符串,只需加入现在扁平化的列表即可:

print(''.join(flatten(a)))

关于python - 在 Python 中将文本与嵌套的 OrderedDict 分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22990716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com