Efficient way to add new rules to a sublist within a nested list of rules











up vote
4
down vote

favorite












I often build nested lists (as a way to keep data sets organised) with some form like:



 nestru = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}};


Here "a" and "b" are first order keys that associate a series of second order keys and values with the first order entity. If I want to add a new set of second order rules I can do something like:



 newru = {"z" -> "x"/"y", "xx" -> 2 "x"} /. nestru[[All, 2]]



{{"z" -> 5/7, "xx" -> 10},



{"z" -> 2/3, "xx" -> 8}}




Then I rebuild the whole data structure again:



MapThread[#1 -> #2 &, {nestru[[All, 1]], 
Flatten[#] & /@ Transpose@{nestru[[All, 2]], newru}}]



{"a" -> {"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10},



"b" -> {"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8}}




This feels like a clunky approach. Is there an 'cleaner'/more efficient way to add new sub-rules to a nested list of rules?










share|improve this question




















  • 2




    Instead of using nested lists, I would suggest to use nested Associations along with AssociateTo.
    – Henrik Schumacher
    Nov 26 at 9:35










  • @HenrikSchumacher If I convert to Association how do I then use AssociateTo to add values to a 'sub'-Association? It's not clear from the documentation.
    – geordie
    Nov 26 at 11:10















up vote
4
down vote

favorite












I often build nested lists (as a way to keep data sets organised) with some form like:



 nestru = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}};


Here "a" and "b" are first order keys that associate a series of second order keys and values with the first order entity. If I want to add a new set of second order rules I can do something like:



 newru = {"z" -> "x"/"y", "xx" -> 2 "x"} /. nestru[[All, 2]]



{{"z" -> 5/7, "xx" -> 10},



{"z" -> 2/3, "xx" -> 8}}




Then I rebuild the whole data structure again:



MapThread[#1 -> #2 &, {nestru[[All, 1]], 
Flatten[#] & /@ Transpose@{nestru[[All, 2]], newru}}]



{"a" -> {"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10},



"b" -> {"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8}}




This feels like a clunky approach. Is there an 'cleaner'/more efficient way to add new sub-rules to a nested list of rules?










share|improve this question




















  • 2




    Instead of using nested lists, I would suggest to use nested Associations along with AssociateTo.
    – Henrik Schumacher
    Nov 26 at 9:35










  • @HenrikSchumacher If I convert to Association how do I then use AssociateTo to add values to a 'sub'-Association? It's not clear from the documentation.
    – geordie
    Nov 26 at 11:10













up vote
4
down vote

favorite









up vote
4
down vote

favorite











I often build nested lists (as a way to keep data sets organised) with some form like:



 nestru = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}};


Here "a" and "b" are first order keys that associate a series of second order keys and values with the first order entity. If I want to add a new set of second order rules I can do something like:



 newru = {"z" -> "x"/"y", "xx" -> 2 "x"} /. nestru[[All, 2]]



{{"z" -> 5/7, "xx" -> 10},



{"z" -> 2/3, "xx" -> 8}}




Then I rebuild the whole data structure again:



MapThread[#1 -> #2 &, {nestru[[All, 1]], 
Flatten[#] & /@ Transpose@{nestru[[All, 2]], newru}}]



{"a" -> {"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10},



"b" -> {"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8}}




This feels like a clunky approach. Is there an 'cleaner'/more efficient way to add new sub-rules to a nested list of rules?










share|improve this question















I often build nested lists (as a way to keep data sets organised) with some form like:



 nestru = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}};


Here "a" and "b" are first order keys that associate a series of second order keys and values with the first order entity. If I want to add a new set of second order rules I can do something like:



 newru = {"z" -> "x"/"y", "xx" -> 2 "x"} /. nestru[[All, 2]]



{{"z" -> 5/7, "xx" -> 10},



{"z" -> 2/3, "xx" -> 8}}




Then I rebuild the whole data structure again:



MapThread[#1 -> #2 &, {nestru[[All, 1]], 
Flatten[#] & /@ Transpose@{nestru[[All, 2]], newru}}]



{"a" -> {"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10},



"b" -> {"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8}}




This feels like a clunky approach. Is there an 'cleaner'/more efficient way to add new sub-rules to a nested list of rules?







list-manipulation replacement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 at 11:11

























asked Nov 26 at 9:07









geordie

1,9661530




1,9661530








  • 2




    Instead of using nested lists, I would suggest to use nested Associations along with AssociateTo.
    – Henrik Schumacher
    Nov 26 at 9:35










  • @HenrikSchumacher If I convert to Association how do I then use AssociateTo to add values to a 'sub'-Association? It's not clear from the documentation.
    – geordie
    Nov 26 at 11:10














  • 2




    Instead of using nested lists, I would suggest to use nested Associations along with AssociateTo.
    – Henrik Schumacher
    Nov 26 at 9:35










  • @HenrikSchumacher If I convert to Association how do I then use AssociateTo to add values to a 'sub'-Association? It's not clear from the documentation.
    – geordie
    Nov 26 at 11:10








2




2




Instead of using nested lists, I would suggest to use nested Associations along with AssociateTo.
– Henrik Schumacher
Nov 26 at 9:35




Instead of using nested lists, I would suggest to use nested Associations along with AssociateTo.
– Henrik Schumacher
Nov 26 at 9:35












@HenrikSchumacher If I convert to Association how do I then use AssociateTo to add values to a 'sub'-Association? It's not clear from the documentation.
– geordie
Nov 26 at 11:10




@HenrikSchumacher If I convert to Association how do I then use AssociateTo to add values to a 'sub'-Association? It's not clear from the documentation.
– geordie
Nov 26 at 11:10










1 Answer
1






active

oldest

votes

















up vote
4
down vote













Let me show you another way. I'd go with associations and I'd make newgru longer but more robust (here "z", or "xx" could be replaced by rules from nestgru if you are not careful).



data = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}} // 
GeneralUtilities`ToAssociations



<|"a" -> <|"x" -> 5, "y" -> 7|>, "b" -> <|"x" -> 4, "y" -> 6|>|>




<|#, "z" -> #x/#y, "xx" -> 2 #x|> & /@ data



<|"a" -> <|"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10|>,
"b" -> <|"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8|>|>







share|improve this answer





















  • It's about time for me to read more about associations... The only problem I see is how to turn it back into a list of rules? If I use Normal only the top level changes to a List. Again It feels like I have to rebuild the data structure if I want utilise lists of rules.
    – geordie
    Nov 26 at 10:43






  • 3




    @geordie don't go back! But if you really need to: data /. Association -> Normal@*Association
    – Kuba
    Nov 26 at 10:51










  • Moving forward, I'll try not to go back (!) but I currently work with large ecosystem built on lists of rules. It may be that I can use most of this will work with Association'ed structures but I'll need to do some testing. For the moment (until I can brush up) 'old school' seems like the safest way to proceed...
    – geordie
    Nov 26 at 10:58








  • 2




    @geordie Just a remark: Associations are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. Thus Association should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also find Dataset interesting.
    – Henrik Schumacher
    Nov 26 at 11:23










  • @HenrikSchumacher, about your insert/delete remark, does it hold for Associations not assigned to a symbol? I have always thought that if an expression isn't assigned to a symbol, the Kernel will make a copy on any modification of this expression to provide immutability...
    – Anton.Sakovich
    Nov 27 at 8:21











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fmathematica.stackexchange.com%2fquestions%2f186699%2fefficient-way-to-add-new-rules-to-a-sublist-within-a-nested-list-of-rules%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








up vote
4
down vote













Let me show you another way. I'd go with associations and I'd make newgru longer but more robust (here "z", or "xx" could be replaced by rules from nestgru if you are not careful).



data = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}} // 
GeneralUtilities`ToAssociations



<|"a" -> <|"x" -> 5, "y" -> 7|>, "b" -> <|"x" -> 4, "y" -> 6|>|>




<|#, "z" -> #x/#y, "xx" -> 2 #x|> & /@ data



<|"a" -> <|"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10|>,
"b" -> <|"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8|>|>







share|improve this answer





















  • It's about time for me to read more about associations... The only problem I see is how to turn it back into a list of rules? If I use Normal only the top level changes to a List. Again It feels like I have to rebuild the data structure if I want utilise lists of rules.
    – geordie
    Nov 26 at 10:43






  • 3




    @geordie don't go back! But if you really need to: data /. Association -> Normal@*Association
    – Kuba
    Nov 26 at 10:51










  • Moving forward, I'll try not to go back (!) but I currently work with large ecosystem built on lists of rules. It may be that I can use most of this will work with Association'ed structures but I'll need to do some testing. For the moment (until I can brush up) 'old school' seems like the safest way to proceed...
    – geordie
    Nov 26 at 10:58








  • 2




    @geordie Just a remark: Associations are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. Thus Association should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also find Dataset interesting.
    – Henrik Schumacher
    Nov 26 at 11:23










  • @HenrikSchumacher, about your insert/delete remark, does it hold for Associations not assigned to a symbol? I have always thought that if an expression isn't assigned to a symbol, the Kernel will make a copy on any modification of this expression to provide immutability...
    – Anton.Sakovich
    Nov 27 at 8:21















up vote
4
down vote













Let me show you another way. I'd go with associations and I'd make newgru longer but more robust (here "z", or "xx" could be replaced by rules from nestgru if you are not careful).



data = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}} // 
GeneralUtilities`ToAssociations



<|"a" -> <|"x" -> 5, "y" -> 7|>, "b" -> <|"x" -> 4, "y" -> 6|>|>




<|#, "z" -> #x/#y, "xx" -> 2 #x|> & /@ data



<|"a" -> <|"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10|>,
"b" -> <|"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8|>|>







share|improve this answer





















  • It's about time for me to read more about associations... The only problem I see is how to turn it back into a list of rules? If I use Normal only the top level changes to a List. Again It feels like I have to rebuild the data structure if I want utilise lists of rules.
    – geordie
    Nov 26 at 10:43






  • 3




    @geordie don't go back! But if you really need to: data /. Association -> Normal@*Association
    – Kuba
    Nov 26 at 10:51










  • Moving forward, I'll try not to go back (!) but I currently work with large ecosystem built on lists of rules. It may be that I can use most of this will work with Association'ed structures but I'll need to do some testing. For the moment (until I can brush up) 'old school' seems like the safest way to proceed...
    – geordie
    Nov 26 at 10:58








  • 2




    @geordie Just a remark: Associations are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. Thus Association should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also find Dataset interesting.
    – Henrik Schumacher
    Nov 26 at 11:23










  • @HenrikSchumacher, about your insert/delete remark, does it hold for Associations not assigned to a symbol? I have always thought that if an expression isn't assigned to a symbol, the Kernel will make a copy on any modification of this expression to provide immutability...
    – Anton.Sakovich
    Nov 27 at 8:21













up vote
4
down vote










up vote
4
down vote









Let me show you another way. I'd go with associations and I'd make newgru longer but more robust (here "z", or "xx" could be replaced by rules from nestgru if you are not careful).



data = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}} // 
GeneralUtilities`ToAssociations



<|"a" -> <|"x" -> 5, "y" -> 7|>, "b" -> <|"x" -> 4, "y" -> 6|>|>




<|#, "z" -> #x/#y, "xx" -> 2 #x|> & /@ data



<|"a" -> <|"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10|>,
"b" -> <|"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8|>|>







share|improve this answer












Let me show you another way. I'd go with associations and I'd make newgru longer but more robust (here "z", or "xx" could be replaced by rules from nestgru if you are not careful).



data = {"a" -> {"x" -> 5, "y" -> 7}, "b" -> {"x" -> 4, "y" -> 6}} // 
GeneralUtilities`ToAssociations



<|"a" -> <|"x" -> 5, "y" -> 7|>, "b" -> <|"x" -> 4, "y" -> 6|>|>




<|#, "z" -> #x/#y, "xx" -> 2 #x|> & /@ data



<|"a" -> <|"x" -> 5, "y" -> 7, "z" -> 5/7, "xx" -> 10|>,
"b" -> <|"x" -> 4, "y" -> 6, "z" -> 2/3, "xx" -> 8|>|>








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 at 9:35









Kuba

103k12200511




103k12200511












  • It's about time for me to read more about associations... The only problem I see is how to turn it back into a list of rules? If I use Normal only the top level changes to a List. Again It feels like I have to rebuild the data structure if I want utilise lists of rules.
    – geordie
    Nov 26 at 10:43






  • 3




    @geordie don't go back! But if you really need to: data /. Association -> Normal@*Association
    – Kuba
    Nov 26 at 10:51










  • Moving forward, I'll try not to go back (!) but I currently work with large ecosystem built on lists of rules. It may be that I can use most of this will work with Association'ed structures but I'll need to do some testing. For the moment (until I can brush up) 'old school' seems like the safest way to proceed...
    – geordie
    Nov 26 at 10:58








  • 2




    @geordie Just a remark: Associations are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. Thus Association should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also find Dataset interesting.
    – Henrik Schumacher
    Nov 26 at 11:23










  • @HenrikSchumacher, about your insert/delete remark, does it hold for Associations not assigned to a symbol? I have always thought that if an expression isn't assigned to a symbol, the Kernel will make a copy on any modification of this expression to provide immutability...
    – Anton.Sakovich
    Nov 27 at 8:21


















  • It's about time for me to read more about associations... The only problem I see is how to turn it back into a list of rules? If I use Normal only the top level changes to a List. Again It feels like I have to rebuild the data structure if I want utilise lists of rules.
    – geordie
    Nov 26 at 10:43






  • 3




    @geordie don't go back! But if you really need to: data /. Association -> Normal@*Association
    – Kuba
    Nov 26 at 10:51










  • Moving forward, I'll try not to go back (!) but I currently work with large ecosystem built on lists of rules. It may be that I can use most of this will work with Association'ed structures but I'll need to do some testing. For the moment (until I can brush up) 'old school' seems like the safest way to proceed...
    – geordie
    Nov 26 at 10:58








  • 2




    @geordie Just a remark: Associations are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. Thus Association should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also find Dataset interesting.
    – Henrik Schumacher
    Nov 26 at 11:23










  • @HenrikSchumacher, about your insert/delete remark, does it hold for Associations not assigned to a symbol? I have always thought that if an expression isn't assigned to a symbol, the Kernel will make a copy on any modification of this expression to provide immutability...
    – Anton.Sakovich
    Nov 27 at 8:21
















It's about time for me to read more about associations... The only problem I see is how to turn it back into a list of rules? If I use Normal only the top level changes to a List. Again It feels like I have to rebuild the data structure if I want utilise lists of rules.
– geordie
Nov 26 at 10:43




It's about time for me to read more about associations... The only problem I see is how to turn it back into a list of rules? If I use Normal only the top level changes to a List. Again It feels like I have to rebuild the data structure if I want utilise lists of rules.
– geordie
Nov 26 at 10:43




3




3




@geordie don't go back! But if you really need to: data /. Association -> Normal@*Association
– Kuba
Nov 26 at 10:51




@geordie don't go back! But if you really need to: data /. Association -> Normal@*Association
– Kuba
Nov 26 at 10:51












Moving forward, I'll try not to go back (!) but I currently work with large ecosystem built on lists of rules. It may be that I can use most of this will work with Association'ed structures but I'll need to do some testing. For the moment (until I can brush up) 'old school' seems like the safest way to proceed...
– geordie
Nov 26 at 10:58






Moving forward, I'll try not to go back (!) but I currently work with large ecosystem built on lists of rules. It may be that I can use most of this will work with Association'ed structures but I'll need to do some testing. For the moment (until I can brush up) 'old school' seems like the safest way to proceed...
– geordie
Nov 26 at 10:58






2




2




@geordie Just a remark: Associations are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. Thus Association should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also find Dataset interesting.
– Henrik Schumacher
Nov 26 at 11:23




@geordie Just a remark: Associations are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. Thus Association should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also find Dataset interesting.
– Henrik Schumacher
Nov 26 at 11:23












@HenrikSchumacher, about your insert/delete remark, does it hold for Associations not assigned to a symbol? I have always thought that if an expression isn't assigned to a symbol, the Kernel will make a copy on any modification of this expression to provide immutability...
– Anton.Sakovich
Nov 27 at 8:21




@HenrikSchumacher, about your insert/delete remark, does it hold for Associations not assigned to a symbol? I have always thought that if an expression isn't assigned to a symbol, the Kernel will make a copy on any modification of this expression to provide immutability...
– Anton.Sakovich
Nov 27 at 8:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • 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.


Use MathJax to format equations. MathJax reference.


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





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2fmathematica.stackexchange.com%2fquestions%2f186699%2fefficient-way-to-add-new-rules-to-a-sublist-within-a-nested-list-of-rules%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

QoS: MAC-Priority for clients behind a repeater

Ивакино (Тотемский район)

Can't locate Autom4te/ChannelDefs.pm in @INC (when it definitely is there)