列表的元素是字典,python语言如何获取指定的字典?
例如:state = [{'jsh':'28754',{‘combine’:‘1’}},{'nsj':'2512165'},{'husdj':'45215'}],,如何提取第一个字典{'jsh':'28754',{‘combine’:‘1’}}
state[0]不就是第一个字典吗
不知道你这个问题是否已经解决, 如果还没有解决的话:可以使用列表索引获取指定字典,如state[0]来取得第一个字典。如果需要特定的键值对,可以使用字典的get()函数或索引来获取。
代码如下:
state = [{'jsh': '28754', 'combine': '1'}, {'jsh': '37201', 'combine': '2'}, {'jsh': '59823', 'combine': '3'}]
# 获取第一个字典
first_dict = state[0]
print(first_dict) # {'jsh': '28754', 'combine': '1'}
# 获取第一个字典中'jsh'键对应的值
jsh_value = first_dict['jsh']
print(jsh_value) # '28754'
# 或使用字典的get()函数获取'combine'键对应的值
combine_value = first_dict.get('combine')
print(combine_value) # '1'