Min and Max of a list of Associations
up vote
3
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
3
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
1 hour ago
add a comment |
up vote
3
down vote
favorite
up vote
3
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 1 hour ago
C. E.
49.1k395197
49.1k395197
New contributor
asked 2 hours ago
Professor Williams
161
161
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
1 hour ago
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
1 hour ago
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
1 hour ago
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
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Apply[Min]]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Apply[Max]]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
You don't needApply
.
– Kuba♦
32 mins ago
add a comment |
up vote
2
down vote
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Apply[Min]]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Apply[Max]]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
You don't needApply
.
– Kuba♦
32 mins ago
add a comment |
up vote
4
down vote
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Apply[Min]]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Apply[Max]]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
You don't needApply
.
– Kuba♦
32 mins ago
add a comment |
up vote
4
down vote
up vote
4
down vote
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Apply[Min]]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Apply[Max]]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
How about
a = {
<|"a" -> 4, "b" -> 9, "c" -> 15|>,
<|"a" -> 21, "b" -> 11, "c" -> 1|>,
<|"a" -> 12, "b" -> 3, "c" -> 21|>
};
Merge[a, Apply[Min]]
(*<|"a" -> 4, "b" -> 3, "c" -> 1|>*)
Merge[a, Apply[Max]]
(*<|"a" -> 21, "b" -> 11, "c" -> 21|>*)
answered 1 hour ago
Shredderroy
1,3331014
1,3331014
You don't needApply
.
– Kuba♦
32 mins ago
add a comment |
You don't needApply
.
– Kuba♦
32 mins ago
You don't need
Apply
.– Kuba♦
32 mins ago
You don't need
Apply
.– Kuba♦
32 mins ago
add a comment |
up vote
2
down vote
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
add a comment |
up vote
2
down vote
Random`Private`MapThreadMin[x]
Random`Private`MapThreadMax[x]
<|"a" -> 4, "b" -> 3, "c" -> 1|>
<|"a" -> 21, "b" -> 11, "c" -> 21|>
add a comment |
up vote
2
down vote
up vote
2
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 1 hour ago
Henrik Schumacher
46.5k466133
46.5k466133
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
1 hour ago