Map a function that takes arguments in different levels of a listMap a function across a list...
Can a human variant take proficiency in initiative?
Why do we need explainable AI?
How would a disabled person earn their living in a medieval-type town?
How to update standalone ESXi / PowerEdge server?
How to say "too quickly", "too recklessly" etc
Different past tense for various *et words
Can the Tasha's Hideous Laughter spell affect a deaf creature?
Using large parts of a research paper
Can UV radiation be safe for the skin?
How did Gollum know Sauron was gathering the Haradrim to make war?
Polarity of gas discharge tubes?
How to find better food in airports
garage light with two hots and one neutral
Can a country avoid prosecution for crimes against humanity by denying it happened?
Divide Numbers by 0
Calculate Landau's function
Why don't "echo -e" commands seem to produce the right output?
What are the French equivalents of "blow away the cobwebs"?
Are there consequences for not filing a DMCA (any country)
How to solve this inequality , when there is a irrational power?
Would there be balance issues if I allowed opportunity attacks against any creature, not just hostile ones?
Given a specific computer system, is it possible to estimate the actual precise run time of a piece of Assembly code
Table alignment (make the content centre)
When did Gwenpool meet Ms. Marvel?
Map a function that takes arguments in different levels of a list
Map a function across a list conditionallyMap a function over the columns of an M x N arraySpan a function across several consecutive elements in a listUsing MapIndexed only at certain elements of a listMapping a function over n levelsMap an Argument to a list of functionsA list of functions taking different argumentsApply a function to a list that is a subset of sublistsMap function over two lists
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
So for example, this is my list of lists:
{{4,1}, {3,1,1}, {2,2,1}}
I would like to map the function to the list of lists to obtain the following outcome:
{ {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}
I would appreciate it if someone could tell me how to do it. Thank you in advance!
map
New contributor
$endgroup$
add a comment |
$begingroup$
I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
So for example, this is my list of lists:
{{4,1}, {3,1,1}, {2,2,1}}
I would like to map the function to the list of lists to obtain the following outcome:
{ {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}
I would appreciate it if someone could tell me how to do it. Thank you in advance!
map
New contributor
$endgroup$
add a comment |
$begingroup$
I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
So for example, this is my list of lists:
{{4,1}, {3,1,1}, {2,2,1}}
I would like to map the function to the list of lists to obtain the following outcome:
{ {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}
I would appreciate it if someone could tell me how to do it. Thank you in advance!
map
New contributor
$endgroup$
I would like to map a function that takes as an argument a list in a list of lists and an element of the list of lists.
So for example, this is my list of lists:
{{4,1}, {3,1,1}, {2,2,1}}
I would like to map the function to the list of lists to obtain the following outcome:
{ {f[{4,1},4], f[{4,1},1]}, {f[{3,1,1},3}], f[{3,1,1},1], f[{3,1,1},1] }, {f[{2,2,1},2], f[{2,2,1},2], f[{2,2,1},1]}
I would appreciate it if someone could tell me how to do it. Thank you in advance!
map
map
New contributor
New contributor
New contributor
asked 8 hours ago
MavatanetMavatanet
261 bronze badge
261 bronze badge
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
Table[Table[f[i, j], {j, i}], {i, lst}]
{{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
f[{2, 2, 1}, 1]}}
$endgroup$
1
$begingroup$
+1. also:Table[f[i, j], {i, lst}, {j, i}]
$endgroup$
– WReach
44 mins ago
add a comment |
$begingroup$
ClearAll[g1, f]
g1 = Function[x, f[x, #] & /@ x]
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
g1 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
Also
ClearAll[g2]
g2[x_]:= Map[f[x, #] &] @ x
g2 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
and
ClearAll[g3]
g3 = Thread[f[#, #], List, {2}] &
g3 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
$endgroup$
add a comment |
$begingroup$
I like the techniques in the other answers (particularly the Table
approach in the answer by MelaGo), but here are a few other options to add to the mix...
Given:
$list = {{4,1}, {3,1,1}, {2,2,1}};
Then any of the following expressions yield the desired result:
Curry[f, 2][#] /@ # & /@ $list
or
Cases[$list, l_ :> (f[l, #] & /@ l)]
or
Replace[$list, l_ :> (f[l, #] & /@ l), {1}]
or
MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]
or
ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]
$endgroup$
add a comment |
Your Answer
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',
autoActivateHeartbeat: false,
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
});
}
});
Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.
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%2f204657%2fmap-a-function-that-takes-arguments-in-different-levels-of-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
Table[Table[f[i, j], {j, i}], {i, lst}]
{{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
f[{2, 2, 1}, 1]}}
$endgroup$
1
$begingroup$
+1. also:Table[f[i, j], {i, lst}, {j, i}]
$endgroup$
– WReach
44 mins ago
add a comment |
$begingroup$
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
Table[Table[f[i, j], {j, i}], {i, lst}]
{{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
f[{2, 2, 1}, 1]}}
$endgroup$
1
$begingroup$
+1. also:Table[f[i, j], {i, lst}, {j, i}]
$endgroup$
– WReach
44 mins ago
add a comment |
$begingroup$
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
Table[Table[f[i, j], {j, i}], {i, lst}]
{{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
f[{2, 2, 1}, 1]}}
$endgroup$
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
Table[Table[f[i, j], {j, i}], {i, lst}]
{{f[{4, 1}, 4], f[{4, 1}, 1]}, {f[{3, 1, 1}, 3], f[{3, 1, 1}, 1],
f[{3, 1, 1}, 1]}, {f[{2, 2, 1}, 2], f[{2, 2, 1}, 2],
f[{2, 2, 1}, 1]}}
answered 8 hours ago
MelaGoMelaGo
2,6361 gold badge1 silver badge7 bronze badges
2,6361 gold badge1 silver badge7 bronze badges
1
$begingroup$
+1. also:Table[f[i, j], {i, lst}, {j, i}]
$endgroup$
– WReach
44 mins ago
add a comment |
1
$begingroup$
+1. also:Table[f[i, j], {i, lst}, {j, i}]
$endgroup$
– WReach
44 mins ago
1
1
$begingroup$
+1. also:
Table[f[i, j], {i, lst}, {j, i}]
$endgroup$
– WReach
44 mins ago
$begingroup$
+1. also:
Table[f[i, j], {i, lst}, {j, i}]
$endgroup$
– WReach
44 mins ago
add a comment |
$begingroup$
ClearAll[g1, f]
g1 = Function[x, f[x, #] & /@ x]
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
g1 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
Also
ClearAll[g2]
g2[x_]:= Map[f[x, #] &] @ x
g2 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
and
ClearAll[g3]
g3 = Thread[f[#, #], List, {2}] &
g3 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
$endgroup$
add a comment |
$begingroup$
ClearAll[g1, f]
g1 = Function[x, f[x, #] & /@ x]
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
g1 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
Also
ClearAll[g2]
g2[x_]:= Map[f[x, #] &] @ x
g2 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
and
ClearAll[g3]
g3 = Thread[f[#, #], List, {2}] &
g3 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
$endgroup$
add a comment |
$begingroup$
ClearAll[g1, f]
g1 = Function[x, f[x, #] & /@ x]
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
g1 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
Also
ClearAll[g2]
g2[x_]:= Map[f[x, #] &] @ x
g2 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
and
ClearAll[g3]
g3 = Thread[f[#, #], List, {2}] &
g3 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
$endgroup$
ClearAll[g1, f]
g1 = Function[x, f[x, #] & /@ x]
lst = {{4, 1}, {3, 1, 1}, {2, 2, 1}};
g1 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
Also
ClearAll[g2]
g2[x_]:= Map[f[x, #] &] @ x
g2 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
and
ClearAll[g3]
g3 = Thread[f[#, #], List, {2}] &
g3 /@ lst
{{f[{4, 1}, 4], f[{4, 1}, 1]},
{f[{3, 1, 1}, 3], f[{3, 1, 1}, 1], f[{3, 1, 1}, 1]},
{f[{2, 2, 1}, 2], f[{2, 2, 1}, 2], f[{2, 2, 1}, 1]}}
edited 7 hours ago
answered 8 hours ago
kglrkglr
215k10 gold badges245 silver badges490 bronze badges
215k10 gold badges245 silver badges490 bronze badges
add a comment |
add a comment |
$begingroup$
I like the techniques in the other answers (particularly the Table
approach in the answer by MelaGo), but here are a few other options to add to the mix...
Given:
$list = {{4,1}, {3,1,1}, {2,2,1}};
Then any of the following expressions yield the desired result:
Curry[f, 2][#] /@ # & /@ $list
or
Cases[$list, l_ :> (f[l, #] & /@ l)]
or
Replace[$list, l_ :> (f[l, #] & /@ l), {1}]
or
MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]
or
ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]
$endgroup$
add a comment |
$begingroup$
I like the techniques in the other answers (particularly the Table
approach in the answer by MelaGo), but here are a few other options to add to the mix...
Given:
$list = {{4,1}, {3,1,1}, {2,2,1}};
Then any of the following expressions yield the desired result:
Curry[f, 2][#] /@ # & /@ $list
or
Cases[$list, l_ :> (f[l, #] & /@ l)]
or
Replace[$list, l_ :> (f[l, #] & /@ l), {1}]
or
MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]
or
ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]
$endgroup$
add a comment |
$begingroup$
I like the techniques in the other answers (particularly the Table
approach in the answer by MelaGo), but here are a few other options to add to the mix...
Given:
$list = {{4,1}, {3,1,1}, {2,2,1}};
Then any of the following expressions yield the desired result:
Curry[f, 2][#] /@ # & /@ $list
or
Cases[$list, l_ :> (f[l, #] & /@ l)]
or
Replace[$list, l_ :> (f[l, #] & /@ l), {1}]
or
MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]
or
ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]
$endgroup$
I like the techniques in the other answers (particularly the Table
approach in the answer by MelaGo), but here are a few other options to add to the mix...
Given:
$list = {{4,1}, {3,1,1}, {2,2,1}};
Then any of the following expressions yield the desired result:
Curry[f, 2][#] /@ # & /@ $list
or
Cases[$list, l_ :> (f[l, #] & /@ l)]
or
Replace[$list, l_ :> (f[l, #] & /@ l), {1}]
or
MapIndexed[f[$list[[#2[[1]]]], #] &, $list, {2}]
or
ReplacePart[$list, {i_, j_} :> f[$list[[i]], $list[[i, j]]]]
answered 23 mins ago
WReachWReach
55.1k2 gold badges119 silver badges219 bronze badges
55.1k2 gold badges119 silver badges219 bronze badges
add a comment |
add a comment |
Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.
Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.
Mavatanet is a new contributor. Be nice, and check out our Code of Conduct.
Mavatanet 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.
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%2f204657%2fmap-a-function-that-takes-arguments-in-different-levels-of-a-list%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