Min and Max of a list of Associations
up vote
8
down vote
favorite
I am trying to find the minimum values for all elements in a list of associations, below is an example
x = {<|"a"-> 4, "b"->9, "c"->15|>, <|"a"->21, "b"->11, "c"->1|>, <|"a"->12, "b"->3, "c"->21|>}
Required output for Min
{<|"a"-> 2, "b"->3, "c"->1|>}
Required output for Max
{<|"a"-> 21, "b"->11, "c"->15|>}
My attempt
for Max: MaximalBy[Values]@x
Result: {<|"a" -> 21, "b" -> 11, "c" -> 1|>}
for Min: MinimalBy[Values]@x
Result: {<|"a" -> 4, "b" -> 9, "c" -> 15|>}
Is there an elegant way to achieve this result?
list-manipulation associations
New contributor
add a comment |
up vote
8
down vote
favorite
I am trying to find the minimum values for all elements in a list of associations, below is an example
x = {<|"a"-> 4, "b"->9, "c"->15|>, <|"a"->21, "b"->11, "c"->1|>, <|"a"->12, "b"->3, "c"->21|>}
Required output for Min
{<|"a"-> 2, "b"->3, "c"->1|>}
Required output for Max
{<|"a"-> 21, "b"->11, "c"->15|>}
My attempt
for Max: MaximalBy[Values]@x
Result: {<|"a" -> 21, "b" -> 11, "c" -> 1|>}
for Min: MinimalBy[Values]@x
Result: {<|"a" -> 4, "b" -> 9, "c" -> 15|>}
Is there an elegant way to achieve this result?
list-manipulation associations
New contributor
1
I don't get it. How is the keya
getting the value2
? That value does not appear fora
in any of the associations. Similarly, why is the value ofc
not equal to21
in the maximal result?
– Shredderroy
Dec 7 at 6:30
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I am trying to find the minimum values for all elements in a list of associations, below is an example
x = {<|"a"-> 4, "b"->9, "c"->15|>, <|"a"->21, "b"->11, "c"->1|>, <|"a"->12, "b"->3, "c"->21|>}
Required output for Min
{<|"a"-> 2, "b"->3, "c"->1|>}
Required output for Max
{<|"a"-> 21, "b"->11, "c"->15|>}
My attempt
for Max: MaximalBy[Values]@x
Result: {<|"a" -> 21, "b" -> 11, "c" -> 1|>}
for Min: MinimalBy[Values]@x
Result: {<|"a" -> 4, "b" -> 9, "c" -> 15|>}
Is there an elegant way to achieve this result?
list-manipulation associations
New contributor
I am trying to find the minimum values for all elements in a list of associations, below is an example
x = {<|"a"-> 4, "b"->9, "c"->15|>, <|"a"->21, "b"->11, "c"->1|>, <|"a"->12, "b"->3, "c"->21|>}
Required output for Min
{<|"a"-> 2, "b"->3, "c"->1|>}
Required output for Max
{<|"a"-> 21, "b"->11, "c"->15|>}
My attempt
for Max: MaximalBy[Values]@x
Result: {<|"a" -> 21, "b" -> 11, "c" -> 1|>}
for Min: MinimalBy[Values]@x
Result: {<|"a" -> 4, "b" -> 9, "c" -> 15|>}
Is there an elegant way to achieve this result?
list-manipulation associations
list-manipulation associations
New contributor
New contributor
edited Dec 7 at 6:22
C. E.
49.2k395199
49.2k395199
New contributor
asked Dec 7 at 5:39
Professor Williams
432
432
New contributor
New contributor
1
I don't get it. How is the keya
getting the value2
? That value does not appear fora
in any of the associations. Similarly, why is the value ofc
not equal to21
in the maximal result?
– Shredderroy
Dec 7 at 6:30
add a comment |
1
I don't get it. How is the keya
getting the value2
? That value does not appear fora
in any of the associations. Similarly, why is the value ofc
not equal to21
in the maximal result?
– Shredderroy
Dec 7 at 6:30
1
1
I don't get it. How is the key
a
getting the value 2
? That value does not appear for a
in any of the associations. Similarly, why is the value of c
not equal to 21
in the maximal result?– Shredderroy
Dec 7 at 6:30
I don't get it. How is the key
a
getting the value 2
? That value does not appear for a
in any of the associations. Similarly, why is the value of c
not equal to 21
in the maximal result?– Shredderroy
Dec 7 at 6:30
add a comment |
3 Answers
3
active
oldest
votes
up vote
11
down vote
accepted
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Min]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Max]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
EDIT
Improved, as per Kuba's suggestion.
add a comment |
up vote
6
down vote
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
1
is there any list out there for these "hidden" commands? Or do you happen to have a favourite list that you can share with us?
– Soner
Dec 7 at 12:33
1
A good try is always?*
*` ;) Honestly, I learned this trick very recently from Carl Woll. Some other good place to look for such things is What are some useful undocumented Mathematica functions?
– Henrik Schumacher
Dec 7 at 13:45
Thanks, I am checking the link right now :)
– Soner
Dec 7 at 21:35
add a comment |
up vote
1
down vote
You may use Query
and MinMax
.
With x
as in OP.
Query[Transpose /* Map[MinMax]]@x
<|"a" -> {4, 21}, "b" -> {3, 11}, "c" -> {1, 21}|>
You can produce a more descriptive result with AssociationThread
.
res = Query[Transpose /* Map[AssociationThread[{"Min", "Max"}, MinMax@#] &]]@x
<|"a" -> <|"Min" -> 4, "Max" -> 21|>,
"b" -> <|"Min" -> 3, "Max" -> 11|>,
"c" -> <|"Min" -> 1, "Max" -> 21|>|>
Which you can then access by Key
with Association
's syntax sugar.
res["a", "Max"]
21
Hope this helps.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Min]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Max]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
EDIT
Improved, as per Kuba's suggestion.
add a comment |
up vote
11
down vote
accepted
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Min]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Max]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
EDIT
Improved, as per Kuba's suggestion.
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Min]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Max]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
EDIT
Improved, as per Kuba's suggestion.
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Min]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Max]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
EDIT
Improved, as per Kuba's suggestion.
edited Dec 7 at 8:30
answered Dec 7 at 6:22
Shredderroy
1,4381115
1,4381115
add a comment |
add a comment |
up vote
6
down vote
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
1
is there any list out there for these "hidden" commands? Or do you happen to have a favourite list that you can share with us?
– Soner
Dec 7 at 12:33
1
A good try is always?*
*` ;) Honestly, I learned this trick very recently from Carl Woll. Some other good place to look for such things is What are some useful undocumented Mathematica functions?
– Henrik Schumacher
Dec 7 at 13:45
Thanks, I am checking the link right now :)
– Soner
Dec 7 at 21:35
add a comment |
up vote
6
down vote
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
1
is there any list out there for these "hidden" commands? Or do you happen to have a favourite list that you can share with us?
– Soner
Dec 7 at 12:33
1
A good try is always?*
*` ;) Honestly, I learned this trick very recently from Carl Woll. Some other good place to look for such things is What are some useful undocumented Mathematica functions?
– Henrik Schumacher
Dec 7 at 13:45
Thanks, I am checking the link right now :)
– Soner
Dec 7 at 21:35
add a comment |
up vote
6
down vote
up vote
6
down vote
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
answered Dec 7 at 6:34
Henrik Schumacher
46.8k466134
46.8k466134
1
is there any list out there for these "hidden" commands? Or do you happen to have a favourite list that you can share with us?
– Soner
Dec 7 at 12:33
1
A good try is always?*
*` ;) Honestly, I learned this trick very recently from Carl Woll. Some other good place to look for such things is What are some useful undocumented Mathematica functions?
– Henrik Schumacher
Dec 7 at 13:45
Thanks, I am checking the link right now :)
– Soner
Dec 7 at 21:35
add a comment |
1
is there any list out there for these "hidden" commands? Or do you happen to have a favourite list that you can share with us?
– Soner
Dec 7 at 12:33
1
A good try is always?*
*` ;) Honestly, I learned this trick very recently from Carl Woll. Some other good place to look for such things is What are some useful undocumented Mathematica functions?
– Henrik Schumacher
Dec 7 at 13:45
Thanks, I am checking the link right now :)
– Soner
Dec 7 at 21:35
1
1
is there any list out there for these "hidden" commands? Or do you happen to have a favourite list that you can share with us?
– Soner
Dec 7 at 12:33
is there any list out there for these "hidden" commands? Or do you happen to have a favourite list that you can share with us?
– Soner
Dec 7 at 12:33
1
1
A good try is always
?*
*` ;) Honestly, I learned this trick very recently from Carl Woll. Some other good place to look for such things is What are some useful undocumented Mathematica functions?– Henrik Schumacher
Dec 7 at 13:45
A good try is always
?*
*` ;) Honestly, I learned this trick very recently from Carl Woll. Some other good place to look for such things is What are some useful undocumented Mathematica functions?– Henrik Schumacher
Dec 7 at 13:45
Thanks, I am checking the link right now :)
– Soner
Dec 7 at 21:35
Thanks, I am checking the link right now :)
– Soner
Dec 7 at 21:35
add a comment |
up vote
1
down vote
You may use Query
and MinMax
.
With x
as in OP.
Query[Transpose /* Map[MinMax]]@x
<|"a" -> {4, 21}, "b" -> {3, 11}, "c" -> {1, 21}|>
You can produce a more descriptive result with AssociationThread
.
res = Query[Transpose /* Map[AssociationThread[{"Min", "Max"}, MinMax@#] &]]@x
<|"a" -> <|"Min" -> 4, "Max" -> 21|>,
"b" -> <|"Min" -> 3, "Max" -> 11|>,
"c" -> <|"Min" -> 1, "Max" -> 21|>|>
Which you can then access by Key
with Association
's syntax sugar.
res["a", "Max"]
21
Hope this helps.
add a comment |
up vote
1
down vote
You may use Query
and MinMax
.
With x
as in OP.
Query[Transpose /* Map[MinMax]]@x
<|"a" -> {4, 21}, "b" -> {3, 11}, "c" -> {1, 21}|>
You can produce a more descriptive result with AssociationThread
.
res = Query[Transpose /* Map[AssociationThread[{"Min", "Max"}, MinMax@#] &]]@x
<|"a" -> <|"Min" -> 4, "Max" -> 21|>,
"b" -> <|"Min" -> 3, "Max" -> 11|>,
"c" -> <|"Min" -> 1, "Max" -> 21|>|>
Which you can then access by Key
with Association
's syntax sugar.
res["a", "Max"]
21
Hope this helps.
add a comment |
up vote
1
down vote
up vote
1
down vote
You may use Query
and MinMax
.
With x
as in OP.
Query[Transpose /* Map[MinMax]]@x
<|"a" -> {4, 21}, "b" -> {3, 11}, "c" -> {1, 21}|>
You can produce a more descriptive result with AssociationThread
.
res = Query[Transpose /* Map[AssociationThread[{"Min", "Max"}, MinMax@#] &]]@x
<|"a" -> <|"Min" -> 4, "Max" -> 21|>,
"b" -> <|"Min" -> 3, "Max" -> 11|>,
"c" -> <|"Min" -> 1, "Max" -> 21|>|>
Which you can then access by Key
with Association
's syntax sugar.
res["a", "Max"]
21
Hope this helps.
You may use Query
and MinMax
.
With x
as in OP.
Query[Transpose /* Map[MinMax]]@x
<|"a" -> {4, 21}, "b" -> {3, 11}, "c" -> {1, 21}|>
You can produce a more descriptive result with AssociationThread
.
res = Query[Transpose /* Map[AssociationThread[{"Min", "Max"}, MinMax@#] &]]@x
<|"a" -> <|"Min" -> 4, "Max" -> 21|>,
"b" -> <|"Min" -> 3, "Max" -> 11|>,
"c" -> <|"Min" -> 1, "Max" -> 21|>|>
Which you can then access by Key
with Association
's syntax sugar.
res["a", "Max"]
21
Hope this helps.
answered Dec 7 at 10:30
Edmund
25.2k329100
25.2k329100
add a comment |
add a comment |
Professor Williams is a new contributor. Be nice, and check out our Code of Conduct.
Professor Williams is a new contributor. Be nice, and check out our Code of Conduct.
Professor Williams is a new contributor. Be nice, and check out our Code of Conduct.
Professor Williams is a new contributor. Be nice, and check out our Code of Conduct.
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%2f187472%2fmin-and-max-of-a-list-of-associations%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
1
I don't get it. How is the key
a
getting the value2
? That value does not appear fora
in any of the associations. Similarly, why is the value ofc
not equal to21
in the maximal result?– Shredderroy
Dec 7 at 6:30