【发布时间】:2015-01-22 08:21:49
【问题描述】:
dictionary = {('x1','y1'): [1,2], ('x2','y2'): [4,5], ('x3','y3'): [6,7]}
如何在 Python YAML 中配置这种字典?
【问题讨论】:
-
也许,如果元组中只有字符串,您可以将它们加入 "_".join(a_tuple) 并使用结果字符串作为键。
标签: python dictionary yaml
dictionary = {('x1','y1'): [1,2], ('x2','y2'): [4,5], ('x3','y3'): [6,7]}
如何在 Python YAML 中配置这种字典?
【问题讨论】:
标签: python dictionary yaml
一种选择是创建您的 YAML 文件,例如:
!!python/tuple ['x1','y1']: [1,2]
!!python/tuple ['x2','y2']: [4,5]
!!python/tuple ['x3','y3']: [6,7]
然后加载它:
import yaml
print yaml.load(stream=open("your_file_path", 'r'))
输出:
{('x1', 'y1'): [1, 2], ('x3', 'y3'): [6, 7], ('x2', 'y2'): [4, 5]}
要获得一些价值,您可以使用:
yaml_load[('x1', 'y1')]
如果你想测试它是一个元组,只需使用:
type(yaml_load.keys()[0])
【讨论】: