Search code examples
pythonjsontumblr

How do I find the max value in a dict when Python thinks the values are strings?


I'm working with the Tumblr API -- I want to build a little doo-dad that will pull all of my posts to my various Tumblrs and archive them to my WordPress blog once a week.

So each time the script runs, I want to log the max post id. So I'm trying to get something like this to work:

    jdata = json.loads(rawjson)
    jposts = jdata['response']['posts']
    for post in jposts:
        print post['id'] 
    print max(jposts['id'])

What I'm getting is a lot of post ids (expected) followed by list indices must be integers, not str but if I do print type(post['id']) Python recognizes them as <type 'long'>. So ... what am I doing wrong here?

Here's a snippet of Tumblr's sample output that you can use for rawjson

{
   "meta":{
      "status":200,
      "msg":"OK"
   },
   "response":{
      "blog":{
         "title":"Scipsy",
         "name":"scipsy",
         "posts":8524,
         "url":"http:\/\/scipsy.tumblr.com\/",
         "updated":1365196814,
         "description":"\u0022Science is interesting and if you don\u0027t agree, fuck off\u0022",
         "ask":true,
         "ask_anon":true,
         "is_nsfw":false,
         "share_likes":false
      },
      "posts":[
         {
            "blog_name":"scipsy",
            "id":47218422365,
            "post_url":"http:\/\/scipsy.tumblr.com\/post\/47218422365\/you-are-missed-that-is-all",
            "slug":"you-are-missed-that-is-all",
            "type":"answer",
            "date":"2013-04-05 21:20:14 GMT",
            "timestamp":1365196814,
            "state":"published",
            "format":"html",
            "reblog_key":"slI4NU3a",
            "tags":[
               "maybe I should start another blog",
               "or maybe not",
               "Maybe I have a concussion from all the throws received during judo and I\u0027m not thinking straight",
               "yeah I think that\u0027s it"
            ],
            "short_url":"http:\/\/tmblr.co\/ZW3EPyh_R-9T",
            "highlighted":[

            ],
            "note_count":35,
            "asking_name":"psydoctor8",
            "asking_url":"http:\/\/psydoctor8.tumblr.com\/",
            "question":"you are missed. that is all.",
            "answer":"\u003Cp\u003EAlthough this break from tumblr have had some positive effects on me (I\u2019ve stopped from compulsively sifting through the NASA\u2019s archive like a maniac and I no longer fell the need to stay up all night trying to answer the most absurd questions) I have to admit that I miss all this sciency stuff and I miss a lot the awesome people I&#8217;ve known through this silly blog.\u003C\/p\u003E"
         },
         {
            "blog_name":"scipsy",
            "id":32267453988,
            "post_url":"http:\/\/scipsy.tumblr.com\/post\/32267453988\/in-the-last-months-this-blog-has-experienced-a",
            "slug":"in-the-last-months-this-blog-has-experienced-a",
            "type":"text",
            "date":"2012-09-25 16:31:00 GMT",
            "timestamp":1348590660,
            "state":"published",
            "format":"html",
            "reblog_key":"0EMwke5R",
            "tags":[

            ],
            "short_url":"http:\/\/tmblr.co\/ZW3EPyU3IaOa",
            "highlighted":[

            ],
            "note_count":246,
            "title":null,
            "body":"\u003Cp\u003EIn the last months this blog has experienced a progressive decline in the number of produced posts. There are several reasons that come to my mind to explain why is that, but probably the better one is about the fact that I lost motivation. Unexpectedly, despite the lack of regular updates the blog gained more and more followers. [That&#8217;s flattering, but at the same time makes me suspicious about the relationship between quality of a blog and number of followers.]\u00a0\u003C\/p\u003E\n\u003Cp\u003EAnyway, I always feel a little lost when I follow a blog and it slowly fades away, and then it just stops posting, without saying anything, so I thought to make this post.\u003C\/p\u003E\n\u003Cp\u003EI&#8217;m not going to update \u003Cem\u003Escipsy\u003C\/em\u003E anymore. This could change, but for now I don&#8217;t feel like posting here anymore. I&#8217;m not going to delete it.\u003C\/p\u003E\n\u003Cp\u003EIf someone would like to stay in touch, just send a message or something. This is my mail: \u003Cem\u003Edr.scipsy@gmail.com\u003C\/em\u003E\u003C\/p\u003E\n\u003Cp\u003EIf someone is wondering: &#8220;\u003Cem\u003EWho will fill my dash with science now?\u003C\/em\u003E\u0022 here&#8217;s a list of \u003Cem\u003Esciency\u003C\/em\u003E tumblr I followed:\u003C\/p\u003E\n\u003Cul\u003E\u003Cli\u003E\u003Ca href=\u0022http:\/\/psydoctor8.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Epsydoctor8\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/electricorchid.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eelectricorchid\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/gradmom.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Egradmom\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/www.itsokaytobesmart.com\/\u0022 target=\u0022_blank\u0022\u003Eitsokaytobesmart\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/realcleverscience.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Erealcleverscience\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/scientistintraining.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Escientistintraining\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/crookedindifference.com\/\u0022 target=\u0022_blank\u0022\u003Ecrookedindifference\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/ohyeahdevelopmentalbiology.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eohyeahdevelopmentalbiology\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/scienceisbeauty.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Escienceisbeauty\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/fuckyeahneuroscience.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Efuckyeahneuroscience\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/climateadaptation.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eclimateadaptation\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/exp.lore.com\/\u0022 target=\u0022_blank\u0022\u003Eexp.lore\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/xenogifh.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Exenogifh\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/www.ziyadnazem.info\/\u0022 target=\u0022_blank\u0022\u003Eziyadnazem\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/intothecontinuum.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eintothecontinuum\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/approachingsignificance.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eapproachingsignificance\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/sciencesoup.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Esciencesoup\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/blog.matthen.com\/\u0022 target=\u0022_blank\u0022\u003Ematthen\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/ulaulaman.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eulaulaman\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/mindovermatterzine.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Emindovermatterzine\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/doctorswithoutborders.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Edoctorswithoutborders\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/mothernaturenetwork.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Emothernaturenetwork\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/wnycradiolab.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ewnycradiolab\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/blog.nysci.org\/\u0022 target=\u0022_blank\u0022\u003Enysci\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/amnhnyc.tumblr.com\u0022 target=\u0022_blank\u0022\u003Eamnhnyc\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/discoverynews.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ediscoverynews\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/tumblr.poptech.org\/\u0022 target=\u0022_blank\u0022\u003Epoptech\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/retina.smithsonianmag.com\/\u0022 target=\u0022_blank\u0022\u003Eretina.smithsonianmag\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/onearth.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Eonearth\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/huffpostscience.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ehuffpostscience\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/bpod-mrc.tumblr.com\/\u0022 target=\u0022_blank\u0022\u003Ebpod-mrc\u003C\/a\u003E\u003C\/li\u003E\n\u003Cli\u003E\u003Ca href=\u0022http:\/\/blog.tedx.com\/\u0022 target=\u0022_blank\u0022\u003Etedx\u003C\/a\u003E\u003C\/li\u003E\n\u003C\/ul\u003E\u003Cp\u003EThat&#8217;s it, I think.\u003C\/p\u003E\n\u003Cp\u003E\u003Csmall\u003E\u003Cem\u003ESo long, and thanks for all the fish.\u003C\/em\u003E\u003C\/small\u003E\u003C\/p\u003E"
         }
      ],
      "total_posts":8524
   }
}

Solution

  • jPosts is a list

    jPosts = [{'id':123},{'id':54233},etc] #example 
    

    so you cannot say jPosts['id'] since it is a list not a dictionary, however you could say something like jPosts[0]['id'] since you are now using id on the 0th index of jPost which is a dictionary

    I think you want

    max(jPosts,key=lambda item:item['id']) #compare based on EACH items 'id' field