2013-01-05 29 views
1

我想从加载的字典中获得用户的排序列表或表格。我能够按照以下方式打印它们,但我无法弄清楚如何按照用户名在示例中制作的推文数量降序对它们进行排序。如果我能够做到这一点,我可能会弄清楚如何跟踪用户。谢谢!python从json提取顶级用户名

tweets = urllib2.urlopen("http://search.twitter.com/search.json?q=ECHO&rpp=100") 
tweets_json = tweets.read() 
data = json.loads(tweets_json)                            

for tweet in data['results']:                       
... print tweet['from_user_name']                                        
... print tweet['to_user_name']                       
... print 
+1

没有任何细节,以“data”包含什么,这是无法回答的。即使是API文档的链接也会对此有所帮助。 –

+0

或者你可以提供'data'的细节,或者你可以使用'print getTopUserNamefromJSON(data)' – Netro

+0

对不起,猜测我复制时我错过了前两行。现在添加。谢谢 – Era

回答

0
tweets = data['results'] 
tweets.sort(key=lambda tw: tw['from_user_name'], reverse=True) 

假设tw['from_user_name']包含来自给定用户名鸣叫次数。

如果tw['from_user_name']中包含用户名,而不是那么:

from collections import Counter 

tweets = data['results'] 
count = Counter(tw['from_user_name'] for tw in tweets) 
tweets.sort(key=lambda tw: count[tw['from_user_name']], reverse=True) 

要通过鸣叫的打印数量排名前10位的用户名他们发送,您不需要鸣叫排序:

print("\n".join(count.most_common(10)))