python 字典列表 列表字典

来源:互联网 发布:网络连接服务器 编辑:程序博客网 时间:2024/05/17 22:26

如何访问字典列表,列表字典中的元素?

 

字典列表:一个列表,其元素是字典
获取'65':列表settings中的字典{'default_value': '65', 'name': 'storage_group_near_full_threshold'}中的值
>>> settings=[{'default_value': '65', 'name': 'storage_group_near_full_threshold'}, {'default_value': '85', 'name': 'storage_group_full_threshold'}, {'default_value': '75', 'name': 'ceph_near_full_threshold'}, {'default_value': '90', 'name': 'ceph_full_threshold'}, {'default_value': '100', 'name': 'pg_count_factor'}]
>>> for i in settings:
...     if i['name']=='storage_group_near_full_threshold':
...             print i['default_value']
...
65

列表字典:一个字典,其键值有列表
获取'65':字典settings中的列表['65', '85', '75', '90', '100']中的值
>>> settings={'name': 'settings', 'third': [], 'second': ['65', '85', '75', '90', '100'], 'single': [], 'fourth': [], 'fifth': [], 'first': ['storage_group_near_full_threshold', 'storage_group_full_threshold', 'ceph_near_full_threshold', 'ceph_full_threshold', 'pg_count_factor']}
>>> print settings['first'].index('storage_group_near_full_threshold')
0
>>> index=settings['first'].index('storage_group_near_full_threshold')
>>> print settings['second'][index]
65
0 0