【问题标题】:Accessing information inside a list that is formatted like a dictionary Python访问像字典 Python 一样格式化的列表中的信息
【发布时间】:2014-06-07 11:33:40
【问题描述】:

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

它看起来像字典,但实际上是一个列表。参数。

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

谢谢。

*感谢您的快速回复。

【问题讨论】:

    标签: python list python-3.x twitter dictionary


    【解决方案1】:

    索引位置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'
    >>>
    

    【讨论】:

      【解决方案2】:

      看起来你得到的是解析为 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]。那将是字典,您可以像往常一样访问其中的元素。

      【讨论】:

        【解决方案3】:

        你得到的是一个list,里面有一个字典项。通过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'
        

        附带说明,使用pprint 进行漂亮打印有助于了解数据结构的组成:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-16
          • 1970-01-01
          • 2015-03-20
          • 1970-01-01
          相关资源
          最近更新 更多