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;
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
add a comment |
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
add a comment |
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
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
python sqlite bottle
asked Mar 23 at 19:34
S1M0N_HS1M0N_H
3225
3225
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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:
- Use a defaultdict.
- Use tuple unpacking for additional readability.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- Use a defaultdict.
- Use tuple unpacking for additional readability.
add a comment |
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:
- Use a defaultdict.
- Use tuple unpacking for additional readability.
add a comment |
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:
- Use a defaultdict.
- Use tuple unpacking for additional readability.
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:
- Use a defaultdict.
- Use tuple unpacking for additional readability.
answered Mar 25 at 13:19
ron rothmanron rothman
10.8k42831
10.8k42831
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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