Вопрос

I would like to know which hashtags are used the most in tweets so I did like this

cursor = db.execute("SELECT text FROM myTable WHERE text LIKE '%#%' ")
for row in cursor:      
    for word in (re.findall(r"#(\w+)", row[0])):
        top_hashtags.append(word)
top_hashtags = {i:top_hashtags.count(i) for i in set(top_hashtags)}
print sorted(top_hashtags, key=lambda x: x[0])

The result was wrong .

I used : print sorted(top_hashtags, key=lambda x: x[0]) But I think it is not working like I want .

Это было полезно?

Решение

This is the sort of thing that Counter was built for:

from collections import Counter
c = Counter(top_hashtags)
print(c.most_common(5)) # print 5 most common hashtags with counts

To answer your specific question, to sort dictionary keys by value use:

top_ht_dict = {i:top_hashtags.count(i) for i in set(top_hashtags)}

sorted(top_ht_dict, key=top_ht_dict.get, reverse=True)
                                       # ^ most common first

Другие советы

What you want is print sorted(top_hashtags, key=top_hashtags.get, reverse=True)

This will sort the hashtags according to how many times the hashtag shows up, with the reverse=True causing it to be in reverse order (i.e most common hashtags first in the list)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top