Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当Python认为dict中的值是字符串时,如何在dict中找到最大值?_Python_Json_Tumblr - Fatal编程技术网

当Python认为dict中的值是字符串时,如何在dict中找到最大值?

当Python认为dict中的值是字符串时,如何在dict中找到最大值?,python,json,tumblr,Python,Json,Tumblr,我正在和——我想建立一个小doo-dad,将我所有的帖子拉到我的各种Tumblr中,并每周将它们归档到我的WordPress博客中 因此,每次脚本运行时,我都要记录max post id。因此,我正在尝试这样做: jdata = json.loads(rawjson) jposts = jdata['response']['posts'] for post in jposts: print post['id'] print max(jposts[

我正在和——我想建立一个小doo-dad,将我所有的帖子拉到我的各种Tumblr中,并每周将它们归档到我的WordPress博客中

因此,每次脚本运行时,我都要记录max post id。因此,我正在尝试这样做:

    jdata = json.loads(rawjson)
    jposts = jdata['response']['posts']
    for post in jposts:
        print post['id'] 
    print max(jposts['id'])
我得到的是很多post-id(预期)后跟
列表索引必须是整数,而不是str
,但是如果我使用
打印类型(post['id'])
Python会将它们识别为
。所以我做错了什么

下面是一个片段,可用于
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’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’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’m not going to update \u003Cem\u003Escipsy\u003C\/em\u003E anymore. This could change, but for now I don’t feel like posting here anymore. I’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: “\u003Cem\u003EWho will fill my dash with science now?\u003C\/em\u003E\u0022 here’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’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
   }
}
jPosts是一个列表

jPosts = [{'id':123},{'id':54233},etc] #example 
因此,您不能说
jPosts['id']
,因为它是一个列表而不是一个字典,但是您可以说类似于
jPosts[0]['id']
,因为您现在在jPost的第0个索引上使用id,而是一个字典

我想你想要

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

jPosts = [{'id':123},{'id':54233},etc] #example 
因此,您不能说
jPosts['id']
,因为它是一个列表而不是一个字典,但是您可以说类似于
jPosts[0]['id']
,因为您现在在jPost的第0个索引上使用id,而是一个字典

我想你想要

max(jPosts,key=lambda item:item['id']) #compare based on EACH items 'id' field
您可以使用指定的
参数:

from operator import itemgetter
print max(jPosts, key=itemgetter('id'))['id']  # prints 47218422365
您可以使用指定的
参数:

from operator import itemgetter
print max(jPosts, key=itemgetter('id'))['id']  # prints 47218422365

我几乎在解决方案中使用了itemgetter,但觉得lambda更具可读性。。。但仍然是+1,因为这解决了他的问题issue@JoranBeasley谢谢,你是第一个用正确的答案回答的人,你的答案也是+1:)我使用
lambda
解决方案,但是+1用于指定['id']而不是整个dict。我几乎在我的解决方案中使用了itemgetter,但觉得lambda更具可读性。。。但仍然是+1,因为这解决了他的问题issue@JoranBeasley谢谢,你是第一个用正确的答案回答的人,你的答案也是+1:)我用的是
lambda
解决方案,但是+1用于指定['id']而不是整个句子。阅读lambda,我从来没有正确理解过。但是,此方法返回具有最高id的帖子中所有值的字典。因此,它标识的是正确的帖子,但提供的信息太多。
max(jPosts,key=lambda item:item['id'])['id']
是我一直在寻找的。我希望这就是你想要的:P我只是觉得你可以自己做最后一次飞跃阅读lambda,我一直没有完全理解。但是,此方法返回具有最高id的帖子中所有值的字典。因此,它标识的是正确的帖子,但提供的信息太多。
max(jPosts,key=lambda item:item['id'])['id']
是我一直在寻找的。我原以为这就是你想要的:P我只是觉得你可以自己完成最后一次飞跃