简体   繁体   中英

How do I sort in ascending/descending order this poorly parsed JSON data returned by tweepy in Python?

I'm trying to build myself a program that returns the top two trending topics with tweet amounts send from the given location on twitter. Which I can do through their API but I'm having trouble working with the JSON data return by the API. I am new to the coding scene and I google most of my stuff. I really don't know if there is an easier way to pull info from a JSON data, I googled quite a bit but I failed to handle it.

I whipped up the code below and I'm able to print out the hashtag and the tweet amount but I don't know how I can sort them out in a descending order. Can anyone help me? I tried the sorted function but it only sorts the tweet amount in itself so if 54958 tweets were sent it sorts it into 45589. I failed to compare each tweets sent with themselves.

I failed to detail my question last time and I was blocked two days from asking questions so if I make any mistakes here again please bare with me, thanks!

import tweepy
import json
import time
from private import consumer_key,consumer_secret


def hashtag():
    auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
    api = tweepy.API(auth)
    hashtag = api.trends_place(23424969)
    h_json = json.dumps(hashtag)
    h_dict = json.loads(h_json)
    h_trends = h_dict[0]
    h_trends2 = h_trends['trends']
    h_sondict = h_trends2[0:]
    for tweet in (h_sondict):
        i =+ 1
        a = '{}'.format(tweet['name'])
        b = '{}'.format(tweet['tweet_volume'])
        c = []
        c.append(a)
        c = c[0]
        d = []
        d.append(b)
        print(c,d)

hashtag()

Which returns this;

#SahipsizElazığ ['10822']
#iyikidoğdunSefaReis ['None']
#MenajerimiAra ['None']
#ileri3lü ['None']
#BuGeceSirenlerSaat21 ['32657']
selahattin demirtaş ['None']
Etİtlafına FurkanBahanesi ['None']
KHKZulmü YeterArtık ['34557']
Hasic ['None']
Sergen ['None']
Türkçeyi ['None']
Çağatay Ulusoy ['None']
Prof. Dr. Arif Ersoy ['None']
AhlaksızOyun Bozuldu ['None']
Nsakala ['None']
TEKDER32 Yaşında ['None']
Rıdvan ['None']
SMAlılara BağışYap ['21992']
Welinton ['None']
Mensah ['None']
Rehabilitasyona Sahipçık ['None']
AYMSiyasiDeğil BağımsızKarar ['21637']
Yasin Börü ['None']
Osmanlıca ['None']
Johansen ['None']
ÖğrtmnMüjde Bekliyor ['76120']
Necip ['None']
Lens ['39612']
Zeki Yavru ['None']
#masterchefturkiye ['11174']
#umutaksututuklansın ['15529']
#UzaktanEğitim ['None']
#Şereftir2Eylül ['None']
#reisleşahlanmayadevam ['19021']
#TekeTek ['12866']
#DüşYakamızdanParaAvcısı ['None']
#HekimoğluYeniSezon ['None']
#27Kadın ['16998']
#SiyahKuğu ['None']
#CnnTürkMasası ['None']
#ilaydayasesol ['None']
#DünyaGümüşhanelilerGünü ['None']
#AyşeKocaBabasınıGörmeli ['29864']
#ReisBizimSoyluBizim ['None']
#Eylulgeldi ['None']
#yunuscansuveoğlubulunsun ['13074']
#ZiyaNolduk ['99630']
#BJKvSİV ['None']
#nevükararınıaçıkla ['None']
#eskisehirhibritistiyor ['10720']

You need to be careful initializing a new list in the for loop, it's not saving the variables you're using to be used on the outside. I'm not sure what you want to do with c and d lists, but you need to declare them outside your for loop.

To sort the data, assuming you have a list you can do something like this: sorted([('a', 1),('b', 3),('d', 4), ('x',12)], key=lambda x: x[1]) which will sort the values in ascending order.

I'll let you figure out the implementation of how to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM