通过python获取YAML中具有相同名称的所有节点

通过python获取YAML中具有相同名称的所有节点,python,yaml,Python,Yaml,如何通过python将YAML中定义的同名节点的所有值(例如title)添加到列表中 name: test article: title: title1 paper: title: title2 blog: title: title3 你有没有自己编写的代码来解决这个问题?你希望数据的结构如何?你想要每种类型的标题目录吗:{'title':{'article':'title1','paper':'title2','blog':'title3'}}我只想返回['title1',

如何通过python将YAML中定义的同名节点的所有值(例如title)添加到列表中

name: test
article:
   title: title1
paper:
   title: title2
blog:
   title: title3

你有没有自己编写的代码来解决这个问题?你希望数据的结构如何?你想要每种类型的标题目录吗:
{'title':{'article':'title1','paper':'title2','blog':'title3'}}
我只想返回['title1','title2','title3']
import os
import yaml

# Define the recursive function
def iter( map, match ):
    output = []
    for key, value in map.iteritems():
        if type( value ) == dict:
            output += iter( value, match )
        if key == match:
            output += [ value ]

    return output

f = open( infile, 'r' )
data = yaml.load( f )
f.close()

print iter( data, 'title' )