python - Accessing items in a list -
i collect hashtags coming twitter. reading documentation need entities https://dev.twitter.com/docs/platform-objects/tweets
"entities": { "hashtags":[], "urls":[], "user_mentions":[] }
i'm able access entities dict , hashtags list
for line in iter(my_tweet_file) tweetionary = json.loads(line) print tweetionary["entities"] print tweetionary["entities"]["hashtags"]
but i'm not able parse correctly items inside hashtags list, i'm interested in text values (lin , scot in following example)
[{u'indices': [41, 45], u'text': u'lin'}, {u'indices': [55, 60], u'text': u'scot'}]
i want populate dictionary of text extracted hashtags list.
thanks, denny
you can nicely using built-in 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'] d in extracted]) #note: python 2.x remove brackets around print statements print(count['lin']) print(count.most_common())
output:
1 [('scot', 1), ('lin', 1)]
Comments
Post a Comment