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

2024-10-04 03:29:34 发布

您现在位置:Python中文网/ 问答频道 /正文

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'}]

它看起来像字典,但实际上是一个列表。阿格。在

如何访问特定条目?例如,如果我想要扩展的\u url值,获得它的最佳方法是什么?在

谢谢。在

*感谢您的快速回复。在


Tags: httpscomhttpurl列表twittermediafit
3条回答

看起来您得到的是被解析为python对象的JSON。仔细看-你得到的是一个只有一个元素的列表。在

>>> len([{'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'}])
1

所以你需要做的就是去掉第一个元素,也就是你想要的东西。如果你叫这个东西my_data,那么你需要my_data[0]。这就是字典,你可以像平常一样访问它里面的元素。在

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'
>>>

你得到的是一个^{},里面有一个字典项。通过0索引获取字典:

>>> data = [{'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'}]
>>> type(data)
<type 'list'>
>>> type(data[0])
<type 'dict'>
>>> data[0]['expanded_url']
'http://twitter.com/voxdotcom/status/458708072131592194/photo/1'

另外,用^{}进行打印有助于了解数据结构的组成:

^{pr2}$

相关问题 更多 >