1

I'm trying to get a sorted list or table of users from a loaded dict. I was able to print them as below but I couldn't figure out how to sort them in descending order according to the number of tweets the user name made in the sample. If I'm able to do that I might figure out how to track the to user as well. Thanks!

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  
4

1 に答える 1

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)))
于 2013-01-05T12:23:50.820 に答える