python - 访问格式类似于字典 Python 的列表中的信息

标签 python list python-3.x twitter dictionary

Twitter API 生成如下所示的实体列表:

[{'expanded_url': 'http://twitter.com/voxdotcom/status/458708072131592194/photo/1', 'display_url': 'pic.twitter.com/uc3j0nU8uf', 'url': 'http://t.co/uc3j0nU8uf', 'media_url_https': 'https://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png', 'id_str': '458708071875764224', 'sizes': {'small': {'h': 256, 'resize': 'fit', 'w': 340}, 'large': {'h': 773, 'resize': 'fit', 'w': 1023}, 'medium': {'h': 453, 'resize': 'fit', 'w': 599}, 'thumb': {'h': 150, 'resize': 'crop', 'w': 150}}, 'indices': [88, 110], 'type': 'photo', 'id': 458708071875764224, 'media_url': 'http://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png'}]

它看起来像一个字典,但它实际上是一个列表。精氨酸

如何访问特定条目?例如,如果我想要 expanded_url 值,获取它的最佳方法是什么?

谢谢。

*感谢您的快速回复。

最佳答案

0 位置索引列表以获取字典:

>>> lst = [{'expanded_url': 'http://twitter.com/voxdotcom/status/458708072131592194/photo/1', 'display_url': 'pic.twitter.com/uc3j0nU8uf', 'url': 'http://t.co/uc3j0nU8uf', 'media_url_https': 'https://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png', 'id_str': '458708071875764224', 'sizes': {'small': {'h': 256, 'resize': 'fit', 'w': 340}, 'large': {'h': 773, 'resize': 'fit', 'w': 1023}, 'medium': {'h': 453, 'resize': 'fit', 'w': 599}, 'thumb': {'h': 150, 'resize': 'crop', 'w': 150}}, 'indices': [88, 110], 'type': 'photo', 'id': 458708071875764224, 'media_url': 'http://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png'}]
>>> lst[0]["expanded_url"]
'http://twitter.com/voxdotcom/status/458708072131592194/photo/1'
>>>

关于python - 访问格式类似于字典 Python 的列表中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230669/

相关文章:

java - 如何动态创建XML(java)?

python - 如果输入的数字介于 2 个不同的数字之间,我该如何打印?

python - 在 scikit-learn 中使用 DecisionTreeClassifier 修复 100% 的准确率

python - 用美汤写xml

python - 有没有办法使用 scikit-learn 绘制随机森林的 OOB ROC 曲线?

python - 如何在循环遍历一定数量的元素后创建另一个元素?

python - 在python中显示jpg图像

Java,创建一个条件语句,如果没有元素的名称与输入匹配,则发送消息?

Python 速成类(class) - 外星人入侵 - 错误背景

python - 将 float64 列转换为日期时间 Pandas