Iterating through dictionariesHow to merge two dictionaries in a single expression?How do I efficiently iterate over each entry in a Java Map?How do I sort a list of dictionaries by a value of the dictionary?What is the best way to iterate over a dictionary?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDelete an element from a dictionaryHow to remove a key from a Python dictionary?
Can I make popcorn with any corn?
How much of data wrangling is a data scientist's job?
Can a Cauchy sequence converge for one metric while not converging for another?
How to format long polynomial?
What defenses are there against being summoned by the Gate spell?
What's the point of deactivating Num Lock on login screens?
Character reincarnated...as a snail
What does it mean to describe someone as a butt steak?
How does one intimidate enemies without having the capacity for violence?
Paid for article while in US on F-1 visa?
Arrow those variables!
Modeling an IP Address
Has there ever been an airliner design involving reducing generator load by installing solar panels?
NMaximize is not converging to a solution
LWC SFDX source push error TypeError: LWC1009: decl.moveTo is not a function
Is it inappropriate for a student to attend their mentor's dissertation defense?
Can I ask the recruiters in my resume to put the reason why I am rejected?
infared filters v nd
tikz convert color string to hex value
Are the number of citations and number of published articles the most important criteria for a tenure promotion?
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
RSA: Danger of using p to create q
What's that red-plus icon near a text?
Are astronomers waiting to see something in an image from a gravitational lens that they've already seen in an adjacent image?
Iterating through dictionaries
How to merge two dictionaries in a single expression?How do I efficiently iterate over each entry in a Java Map?How do I sort a list of dictionaries by a value of the dictionary?What is the best way to iterate over a dictionary?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDelete an element from a dictionaryHow to remove a key from a Python dictionary?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:
d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]
Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!
python dictionary
|
show 3 more comments
I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:
d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]
Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!
python dictionary
I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?
– busybear
Mar 9 at 1:05
Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.
– Plasmid
Mar 9 at 1:08
Put d1-d9 in a list?
– wjandrea
Mar 9 at 1:09
@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9
– Plasmid
Mar 9 at 1:11
@Plasmid Yes, use a list comprehension.
– wjandrea
Mar 9 at 1:11
|
show 3 more comments
I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:
d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]
Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!
python dictionary
I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:
d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]
Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!
python dictionary
python dictionary
edited Mar 9 at 1:07
Plasmid
asked Mar 9 at 1:02
PlasmidPlasmid
174
174
I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?
– busybear
Mar 9 at 1:05
Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.
– Plasmid
Mar 9 at 1:08
Put d1-d9 in a list?
– wjandrea
Mar 9 at 1:09
@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9
– Plasmid
Mar 9 at 1:11
@Plasmid Yes, use a list comprehension.
– wjandrea
Mar 9 at 1:11
|
show 3 more comments
I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?
– busybear
Mar 9 at 1:05
Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.
– Plasmid
Mar 9 at 1:08
Put d1-d9 in a list?
– wjandrea
Mar 9 at 1:09
@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9
– Plasmid
Mar 9 at 1:11
@Plasmid Yes, use a list comprehension.
– wjandrea
Mar 9 at 1:11
I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?
– busybear
Mar 9 at 1:05
I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?
– busybear
Mar 9 at 1:05
Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.
– Plasmid
Mar 9 at 1:08
Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.
– Plasmid
Mar 9 at 1:08
Put d1-d9 in a list?
– wjandrea
Mar 9 at 1:09
Put d1-d9 in a list?
– wjandrea
Mar 9 at 1:09
@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9
– Plasmid
Mar 9 at 1:11
@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9
– Plasmid
Mar 9 at 1:11
@Plasmid Yes, use a list comprehension.
– wjandrea
Mar 9 at 1:11
@Plasmid Yes, use a list comprehension.
– wjandrea
Mar 9 at 1:11
|
show 3 more comments
1 Answer
1
active
oldest
votes
You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k
added based on the quotient and remainder of 3 of the number:
[k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]
so that given:
d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']
this returns:
[1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]
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%2f55072984%2fiterating-through-dictionaries%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
You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k
added based on the quotient and remainder of 3 of the number:
[k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]
so that given:
d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']
this returns:
[1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]
add a comment |
You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k
added based on the quotient and remainder of 3 of the number:
[k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]
so that given:
d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']
this returns:
[1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]
add a comment |
You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k
added based on the quotient and remainder of 3 of the number:
[k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]
so that given:
d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']
this returns:
[1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]
You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k
added based on the quotient and remainder of 3 of the number:
[k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]
so that given:
d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']
this returns:
[1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]
answered Mar 9 at 1:14
blhsingblhsing
42.5k41743
42.5k41743
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%2f55072984%2fiterating-through-dictionaries%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
I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?
– busybear
Mar 9 at 1:05
Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.
– Plasmid
Mar 9 at 1:08
Put d1-d9 in a list?
– wjandrea
Mar 9 at 1:09
@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9
– Plasmid
Mar 9 at 1:11
@Plasmid Yes, use a list comprehension.
– wjandrea
Mar 9 at 1:11