Recommended method for grouping sqlite.Row rowsWhich Python memory profiler is recommended?What is the difference between Python's list methods append and extend?Understanding Python super() with __init__() methodsStatic methods in Python?Is it possible to insert multiple rows at a time in an SQLite database?Does Python have a string 'contains' substring method?Select first row in each GROUP BY group?How to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet statistics for each group (such as count, mean, etc) using pandas GroupBy?

What is this dime sized black bug with white on the segments near Loveland Colorodao?

How is dynamic resistance of a diode modeled for large voltage variations?

Is it wise to pay off mortgage with 401k?

How did Arya and the Hound get into King's Landing so easily?

Eigenvalues of the Laplace-Beltrami operator on a compact Riemannnian manifold

Is presenting a play showing Military characters in a bad light a crime in the US?

How to play vs. 1.e4 e5 2.Nf3 Nc6 3.Bc4 d6?

Farthing / Riding

Do most Taxis give Receipts in London?

400–430 degrees Celsius heated bath

Expand a hexagon

List of lists elementwise greater/smaller than

Difference in 1 user doing 1000 iterations and 1000 users doing 1 iteration in Load testing

US F1 Visa grace period attending a conference

How could Dwarves prevent sand from filling up their settlements

How do we properly manage transitions within a descriptive section?

How do we explain the use of a software on a math paper?

What quantum phenomena violate the superposition principle in electromagnetism?

Presenting 2 results for one variable using a left brace

Is my company merging branches wrong?

How to draw with Tikz a chord parallel to AC that passes through a point?

How should I mix small caps with digits or symbols?

Way of refund if scammed?

Why was Houston selected as the location for the Manned Spacecraft Center?



Recommended method for grouping sqlite.Row rows


Which Python memory profiler is recommended?What is the difference between Python's list methods append and extend?Understanding Python super() with __init__() methodsStatic methods in Python?Is it possible to insert multiple rows at a time in an SQLite database?Does Python have a string 'contains' substring method?Select first row in each GROUP BY group?How to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet statistics for each group (such as count, mean, etc) using pandas GroupBy?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Given the following list (from an sqlite query):



[('job1', 'location1', 10), 
('job1', 'location2', 10),
('job2', 'location1', 5),
('job3', 'location1', 10),
('job3', 'location3', 10)]


I'd like to have the following rendered in my tpl template:



job1
location1: 10
location2: 10

job2
location1: 5

job3
location1: 10
location3: 10


I can probably get this done with setdefault



d = 

for job in jobs:
d.setdefault(job[0], ).update(job[1]: job[2])


but I'm wondering what is the standard or best practice way of doing this?



Cheers,










share|improve this question




























    0















    Given the following list (from an sqlite query):



    [('job1', 'location1', 10), 
    ('job1', 'location2', 10),
    ('job2', 'location1', 5),
    ('job3', 'location1', 10),
    ('job3', 'location3', 10)]


    I'd like to have the following rendered in my tpl template:



    job1
    location1: 10
    location2: 10

    job2
    location1: 5

    job3
    location1: 10
    location3: 10


    I can probably get this done with setdefault



    d = 

    for job in jobs:
    d.setdefault(job[0], ).update(job[1]: job[2])


    but I'm wondering what is the standard or best practice way of doing this?



    Cheers,










    share|improve this question
























      0












      0








      0








      Given the following list (from an sqlite query):



      [('job1', 'location1', 10), 
      ('job1', 'location2', 10),
      ('job2', 'location1', 5),
      ('job3', 'location1', 10),
      ('job3', 'location3', 10)]


      I'd like to have the following rendered in my tpl template:



      job1
      location1: 10
      location2: 10

      job2
      location1: 5

      job3
      location1: 10
      location3: 10


      I can probably get this done with setdefault



      d = 

      for job in jobs:
      d.setdefault(job[0], ).update(job[1]: job[2])


      but I'm wondering what is the standard or best practice way of doing this?



      Cheers,










      share|improve this question














      Given the following list (from an sqlite query):



      [('job1', 'location1', 10), 
      ('job1', 'location2', 10),
      ('job2', 'location1', 5),
      ('job3', 'location1', 10),
      ('job3', 'location3', 10)]


      I'd like to have the following rendered in my tpl template:



      job1
      location1: 10
      location2: 10

      job2
      location1: 5

      job3
      location1: 10
      location3: 10


      I can probably get this done with setdefault



      d = 

      for job in jobs:
      d.setdefault(job[0], ).update(job[1]: job[2])


      but I'm wondering what is the standard or best practice way of doing this?



      Cheers,







      python sqlite bottle






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 23 at 19:34









      S1M0N_HS1M0N_H

      3225




      3225






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Here's how I'd make your code more Pythonic:



          from collections import defaultdict

          d = defaultdict(dict)

          for (job_id, location, value) in jobs:
          d[job_id][location] = value

          # if you need an actual dict at the end (and not a defaultdict),
          # use d = dict(d)


          What I changed:



          1. Use a defaultdict.

          2. Use tuple unpacking for additional readability.





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317602%2frecommended-method-for-grouping-sqlite-row-rows%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Here's how I'd make your code more Pythonic:



            from collections import defaultdict

            d = defaultdict(dict)

            for (job_id, location, value) in jobs:
            d[job_id][location] = value

            # if you need an actual dict at the end (and not a defaultdict),
            # use d = dict(d)


            What I changed:



            1. Use a defaultdict.

            2. Use tuple unpacking for additional readability.





            share|improve this answer



























              1














              Here's how I'd make your code more Pythonic:



              from collections import defaultdict

              d = defaultdict(dict)

              for (job_id, location, value) in jobs:
              d[job_id][location] = value

              # if you need an actual dict at the end (and not a defaultdict),
              # use d = dict(d)


              What I changed:



              1. Use a defaultdict.

              2. Use tuple unpacking for additional readability.





              share|improve this answer

























                1












                1








                1







                Here's how I'd make your code more Pythonic:



                from collections import defaultdict

                d = defaultdict(dict)

                for (job_id, location, value) in jobs:
                d[job_id][location] = value

                # if you need an actual dict at the end (and not a defaultdict),
                # use d = dict(d)


                What I changed:



                1. Use a defaultdict.

                2. Use tuple unpacking for additional readability.





                share|improve this answer













                Here's how I'd make your code more Pythonic:



                from collections import defaultdict

                d = defaultdict(dict)

                for (job_id, location, value) in jobs:
                d[job_id][location] = value

                # if you need an actual dict at the end (and not a defaultdict),
                # use d = dict(d)


                What I changed:



                1. Use a defaultdict.

                2. Use tuple unpacking for additional readability.






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 25 at 13:19









                ron rothmanron rothman

                10.8k42831




                10.8k42831





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317602%2frecommended-method-for-grouping-sqlite-row-rows%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript