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?
list-manipulation replacement
add a comment |
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?
list-manipulation replacement
2
Instead of using nested lists, I would suggest to use nestedAssociation
s along withAssociateTo
.
– Henrik Schumacher
Nov 26 at 9:35
@HenrikSchumacher If I convert toAssociation
how do I then useAssociateTo
to add values to a 'sub'-Association
? It's not clear from the documentation.
– geordie
Nov 26 at 11:10
add a comment |
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?
list-manipulation replacement
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
list-manipulation replacement
edited Nov 26 at 11:11
asked Nov 26 at 9:07
geordie
1,9681530
1,9681530
2
Instead of using nested lists, I would suggest to use nestedAssociation
s along withAssociateTo
.
– Henrik Schumacher
Nov 26 at 9:35
@HenrikSchumacher If I convert toAssociation
how do I then useAssociateTo
to add values to a 'sub'-Association
? It's not clear from the documentation.
– geordie
Nov 26 at 11:10
add a comment |
2
Instead of using nested lists, I would suggest to use nestedAssociation
s along withAssociateTo
.
– Henrik Schumacher
Nov 26 at 9:35
@HenrikSchumacher If I convert toAssociation
how do I then useAssociateTo
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
Association
s along with AssociateTo
.– Henrik Schumacher
Nov 26 at 9:35
Instead of using nested lists, I would suggest to use nested
Association
s 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
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
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|>|>
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 useNormal
only the top level changes to aList
. 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 withAssociation
'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:Association
s are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. ThusAssociation
should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also findDataset
interesting.
– Henrik Schumacher
Nov 26 at 11:23
@HenrikSchumacher, about your insert/delete remark, does it hold forAssociation
s 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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
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|>|>
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 useNormal
only the top level changes to aList
. 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 withAssociation
'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:Association
s are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. ThusAssociation
should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also findDataset
interesting.
– Henrik Schumacher
Nov 26 at 11:23
@HenrikSchumacher, about your insert/delete remark, does it hold forAssociation
s 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
add a comment |
up vote
4
down vote
accepted
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|>|>
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 useNormal
only the top level changes to aList
. 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 withAssociation
'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:Association
s are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. ThusAssociation
should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also findDataset
interesting.
– Henrik Schumacher
Nov 26 at 11:23
@HenrikSchumacher, about your insert/delete remark, does it hold forAssociation
s 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
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
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|>|>
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|>|>
answered Nov 26 at 9:35
Kuba♦
103k12201513
103k12201513
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 useNormal
only the top level changes to aList
. 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 withAssociation
'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:Association
s are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. ThusAssociation
should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also findDataset
interesting.
– Henrik Schumacher
Nov 26 at 11:23
@HenrikSchumacher, about your insert/delete remark, does it hold forAssociation
s 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
add a comment |
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 useNormal
only the top level changes to aList
. 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 withAssociation
'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:Association
s are essentially hash tables, so -- in contrast to lists -- operations like insert and delete do not need a full copy of the data structure. ThusAssociation
should not only be much more convenient but also much more efficient. However, they come with a certain extra memory cost. You might also findDataset
interesting.
– Henrik Schumacher
Nov 26 at 11:23
@HenrikSchumacher, about your insert/delete remark, does it hold forAssociation
s 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:
Association
s 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:
Association
s 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
Association
s 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
Association
s 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
add a comment |
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.
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%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
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
2
Instead of using nested lists, I would suggest to use nested
Association
s along withAssociateTo
.– Henrik Schumacher
Nov 26 at 9:35
@HenrikSchumacher If I convert to
Association
how do I then useAssociateTo
to add values to a 'sub'-Association
? It's not clear from the documentation.– geordie
Nov 26 at 11:10