对于字典列表,假设:
[{'points':50, 'time': '5:00', 'year': 2010},
{'points': 25, 'time': '6:00', 'month': "february"},
{'points':90, 'time': '9:00', 'month': 'january'},
{'points_h1':20, 'month': 'june'}]
有没有办法从中只提取最后一本字典?
-1索引将给出最后一个元素
d=[{'points':50, 'time': '5:00', 'year': 2010},
{'points': 25, 'time': '6:00', 'month': "february"},
{'points':90, 'time': '9:00', 'month': 'january'},
{'points_h1':20, 'month': 'june'}]
print(d[-1])
您也可以先反转列表,然后访问第一个字典(实际上是最后一个字典(。
lst_dicts = [{'points': 50, 'time': '5:00', 'year': 2010},
{'points': 25, 'time': '6:00', 'month': "february"},
{'points': 90, 'time': '9:00', 'month': 'january'},
{'points_h1': 20, 'month': 'june'}]
res=lst_dicts[::-1] #reversing the list
print(res[0])