【问题标题】:Accessing items in a list访问列表中的项目
【发布时间】:2013-05-07 06:03:46
【问题描述】:

我想收集一些来自 Twitter 的主题标签。 阅读我需要获取实体的文档 https://dev.twitter.com/docs/platform-objects/tweets

"entities":
{
    "hashtags":[],
    "urls":[],
    "user_mentions":[]
}

我目前能够访问实体字典和主题标签列表

for line in iter(my_tweet_file)
    tweetionary = json.loads(line)
    print tweetionary["entities"]
    print tweetionary["entities"]["hashtags"]

但我无法正确解析主题标签列表中的项目,我对文本值感兴趣(以下示例中为 lin 和 Scot)

[{u'indices': [41, 45], u'text': u'lin'}, {u'indices': [55, 60], u'text': u'Scot'}]

我想填充从主题标签列表中提取的文本字典。

谢谢,丹尼

【问题讨论】:

  • 准确,需要什么。 从主题标签列表中提取的文本字典 - 这不清楚。字典意味着,它有键:值。需要什么键和值?也许您只需要一个标签列表?
  • 您希望您的最终字典看起来如何?你能给出一些示例键和值吗?目前您只有主题标签,可以存储在列表中
  • 第一步,我想提取每个主题标签,但是因为我需要计算它们的频率,所以我想像这样的字典:codefreq_hash = {'lin' :1, 'Scot':1, 'Win':3} code

标签: python list dictionary hashtag


【解决方案1】:

您可以使用内置的Counter() 很好地做到这一点:

from collections import Counter

extracted = [{u'indices': [41, 45], u'text': u'lin'},
             {u'indices': [55, 60], u'text': u'Scot'}]

count = Counter([d['text'] for d in extracted])

#Note: For python 2.x remove brackets around print statements
print(count['lin'])
print(count.most_common())

输出:

1
[('Scot', 1), ('lin', 1)]

【讨论】:

  • 我要记录一下关于 Counter 的自我:无论如何,我仍然对如何从这样的简单打印中直接访问文本值(lin 和 Scot)存有疑问:codeprint tweetionary [“实体”][“标签”]....??? code
  • 我有一个 for 循环逐行阅读我的推文信息,所以一旦我阅读其他主题标签,如何更新提取的结构。循环结束时将使用 count = Counter(...) 行。
  • 您使用什么命令从我的示例中获取extracted 代码 sn-p?我认为您可以将for d in extracted 替换为for d in <your_command_here>。可能是for d in tweetionary["entities"]["hashtags"] 但很难说
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多