- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的 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/
我像那样遍历数组。 NSArray *array = [[currentRaum raumattribute] allObjects]; NSString *compositeString =
我想找到所有引用这种模式的子字符串:一些字符+一些字符+第一个字符。现在我在 Python 2.7 中有了这个: T = "i was here" m = re.findall(r"([a-z])[a
我想使用与 tidyr 分开将一列字符串(例如 [1, 58, 10] )分成几列。我的问题是有时列较短(永远不会更长)。我在同一个数据框中有很多列有这个问题。 加载包 require(tidyr)
我正在开发一个具有图形用户界面的网络测试工具。我现在面临的问题是,我无法将基础数据与 GUI 类分开。该应用程序由一个 QMainWindow 组成,它随后生成多个其他 QDialogs 并具有一些
我经常听到“策略与机制分离”的口头禅,尤其是在 Unix 哲学的背景下。这是什么意思,有哪些具体的例子?什么时候/为什么是/不是一件好事? 最佳答案 它基本上是将需求或业务功能与技术实现分离。机制是技
我正在使用 writeToFile:atomically: 方法将一些加密数据写入文本文件。问题是,需要保存的文件必须是用户加密的文件,并带有我选择的扩展名。这是我到目前为止所拥有的: [encryp
我有这串 abcdef x y z 或这个 "ab cd ef" x y z 我正试图将其解析为 s1 = "abcdef" arr = ["x","y","z"] 或者 s1 = "ab cd e
这个问题已经有答案了: One big javascript file or multiple smaller files? [duplicate] (7 个回答) 已关闭 6 年前。 我有 4 种类
我有这样的事情 - function DetailCtrl($scope) { $scope.persons = [{ id: 1, name: "Mark"
在操作(复制/移动)包含合并单元格的范围时,我总是收到错误消息“您的粘贴与合并单元格重叠。请取消合并单元格,然后重试”。但是,当尝试使用 Range#breakApart 取消合并范围内的单元格时,我
我有一个包含一些 TextFields 的 TableView。所述 TextFields 的值链接到二维数组(NSMutableArrays 的 NSArray)中的某些位置。 一个初始的干净数组定
我定义了一个标签,其中一半需要在左侧,另一半文本需要在右侧。我怎样才能解决这个问题,让另一半拉对? 我添加了 margin-right 以使文本向右拉,但它与其他 div 不一致。
我正在尝试创建一个正则表达式来将 JavaScript 中的每个单词与 .(点)分开。 function myFunction() { var url = "in.k1.k2.k3.k4.com"
如何使用 CSS 将网站的正文/内容区域与背景分开。为了向您展示我的意思,请看附图。因此,两侧的背景将扩展到拥有超大显示器的人,但内容将始终保持相同大小。 谢谢,阿马尔 http://i.imgur.
有可能用 CSS 将两个背景图像对 Angular 分开吗? 我知道如何只用一张图片制作它,但我不能用两张图片制作它。 这是一个例子: |-------------| | /|
这是一个JSFiddle我创建了展示代码的外观。我将如何给予这些 它们之间是否存在间隙,没有一个元素低于另一个元素? .main-content { width: 50%; float: le
我正在处理具有这样数据的项目(我使用带有 python 的 pandas 框架): days rain 0 1 2 0 3 1 1
我正在尝试编写一个宏来获取信息并将该信息发送到另一个函数,方法是将原始 va_list 拆分为字符串,然后从原始 va_list 生成另一个 va_list。 下面是我的代码。 调用宏 /* Usag
我需要来自 SharedToDomains 和 SharedFromDomains 的键和值数据。我想打印这些值。 var LogResponse = DeserializeFromJson(sLog
我现在正在使用 Alamofire 来发出发布请求。我首先在 ViewController 中构建它并开始工作。但后来我试图通过在另一个 class 中构建它来分离它。我使用 singleton 并且
我是一名优秀的程序员,十分优秀!