访问 YAML 中的嵌套元素并输出有序的唯一 python 列表
Access nested elements in YAML and output ordered unique python list
我正在尝试解析 yaml-file 以将嵌套的 child-elements 输出到有序的唯一 python 列表中,该列表不包含重复值。我的输入 yaml-file 是:
# example.yml
name_1:
parameters:
- soccer
- football
- basketball
- cricket
- hockey
- table tennis
tag:
- navigation
assets:
- url
name_2:
parameters:
- soccer
- rugby
- swimming
examples:
- use case 1
- use case 2
- use case 3
我设法打印出所有 parents 中的第一个 child,它们是:
['assets', 'examples', 'parameters', 'tag']
使用以下代码:
import yaml
with open(r'/Users/.../example.yml') as file:
documents = yaml.full_load(file)
a_list = []
for item, doc in documents.items():
a_list.extend(doc)
res = list(set(a_list))
res.sort()
print(res)
我正在努力扩展脚本以获得 parameters
元素下方的以下有序唯一列表:
['basketball', 'cricket', 'football', 'hockey', 'rugby', 'soccer', 'swimming', 'table tennis']
提前感谢您的任何建议!
我可以通过迭代 parameters
键 -
得到这个
import yaml
with open(r'example.yaml') as file:
documents = yaml.full_load(file)
a_list = []
a_vals=[]
for item, doc, in documents.items():
for val in doc['parameters']:
a_vals.append(val)
a_list.extend(doc)
res = list(set(a_list))
res.sort()
a_vals=list(set(a_vals))
a_vals.sort()
print(a_vals)
print(res)
输出-
python.exe "pysuperclass.py"
['basketball', 'cricket', 'football', 'hockey', 'rugby', 'soccer', 'swimming', 'table tennis']
['assets', 'examples', 'parameters', 'tag']
不需要中间列表,直接添加到集合即可。
import yaml
with open("example.yml") as fh:
documents = yaml.full_load(fh)
params = set()
for key in documents.keys():
params.update(documents[key]["parameters"])
print(sorted(params))
我正在尝试解析 yaml-file 以将嵌套的 child-elements 输出到有序的唯一 python 列表中,该列表不包含重复值。我的输入 yaml-file 是:
# example.yml
name_1:
parameters:
- soccer
- football
- basketball
- cricket
- hockey
- table tennis
tag:
- navigation
assets:
- url
name_2:
parameters:
- soccer
- rugby
- swimming
examples:
- use case 1
- use case 2
- use case 3
我设法打印出所有 parents 中的第一个 child,它们是:
['assets', 'examples', 'parameters', 'tag']
使用以下代码:
import yaml
with open(r'/Users/.../example.yml') as file:
documents = yaml.full_load(file)
a_list = []
for item, doc in documents.items():
a_list.extend(doc)
res = list(set(a_list))
res.sort()
print(res)
我正在努力扩展脚本以获得 parameters
元素下方的以下有序唯一列表:
['basketball', 'cricket', 'football', 'hockey', 'rugby', 'soccer', 'swimming', 'table tennis']
提前感谢您的任何建议!
我可以通过迭代 parameters
键 -
import yaml
with open(r'example.yaml') as file:
documents = yaml.full_load(file)
a_list = []
a_vals=[]
for item, doc, in documents.items():
for val in doc['parameters']:
a_vals.append(val)
a_list.extend(doc)
res = list(set(a_list))
res.sort()
a_vals=list(set(a_vals))
a_vals.sort()
print(a_vals)
print(res)
输出-
python.exe "pysuperclass.py"
['basketball', 'cricket', 'football', 'hockey', 'rugby', 'soccer', 'swimming', 'table tennis']
['assets', 'examples', 'parameters', 'tag']
不需要中间列表,直接添加到集合即可。
import yaml
with open("example.yml") as fh:
documents = yaml.full_load(fh)
params = set()
for key in documents.keys():
params.update(documents[key]["parameters"])
print(sorted(params))