Accessing information inside a list that is format

2019-08-02 18:29发布

The Twitter API spits out lists for entities that look like this:

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

It looks like a dictionary but it is in fact a list. Arg.

How can I access specific entries? For example, if I want the expanded_url value, what is the best way to get it?

Thanks.

*Thanks for the fast replies.

3条回答
一纸荒年 Trace。
2楼-- · 2019-08-02 18:49

What you get is a list with one dictionary item inside. Get the dictionary by 0 index:

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

As a side note, pretty-printing with pprint helps to see what the data structure consists of:

>>> from pprint import pprint
>>> pprint(data)
[{'display_url': 'pic.twitter.com/uc3j0nU8uf',
  'expanded_url': 'http://twitter.com/voxdotcom/status/458708072131592194/photo/1',
  'id': 458708071875764224,
  'id_str': '458708071875764224',
  'indices': [88, 110],
  'media_url': 'http://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png',
  'media_url_https': 'https://pbs.twimg.com/media/Bl2oj5_CYAAO72v.png',
  'sizes': {'large': {'h': 773, 'resize': 'fit', 'w': 1023},
            'medium': {'h': 453, 'resize': 'fit', 'w': 599},
            'small': {'h': 256, 'resize': 'fit', 'w': 340},
            'thumb': {'h': 150, 'resize': 'crop', 'w': 150}},
  'type': 'photo',
  'url': 'http://t.co/uc3j0nU8uf'}]
查看更多
Anthone
3楼-- · 2019-08-02 18:50

Index the list at position 0 to get the dictionary:

>>> 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'
>>>
查看更多
老娘就宠你
4楼-- · 2019-08-02 19:05

It looks like what you got back is JSON parsed as a python object. Look closely - what you have is a list with only one element in it.

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

So all you need to do is take out the first element, which is the stuff you want. If you're calling this thing my_data, then you want my_data[0]. That'll be the dictionary, and you can access the elements inside it as you normally would.

查看更多
登录 后发表回答