Selecting elements from a list where the order is set by another listHow to order a list to match the order of another list?Creating sums of elements from a listGenerating an ordered list of pairs of elements from ordered listsGenerating a random sample of elements from a list that includes a subset of the listDeleteDuplicates Delete elements of a listMatching the order of a master list of lists from a random list of listshow to apply Alternatives command to delete cases from a list?Adding multiplied corresponding elements from two different listsDelete elements of a list that appear in another listRemoving zeros from a list of equationsSelecting elements satisfying a condition
How can I get people to remember my character's gender?
Correct way of drawing empty, half-filled and fully filled circles?
Is an HNN extension of a virtually torsion-free group virtually torsion-free?
Is the book wrong about the Nyquist Sampling Criterion?
My first C++ game (snake console game)
Why do these characters still seem to be the same age after the events of Endgame?
How long would it take for people to notice a mass disappearance?
How should I tell my manager I'm not paying for an optional after work event I'm not going to?
Are pressure-treated posts that have been submerged for a few days ruined?
What do I do if my advisor made a mistake?
How do I calculate how many of an item I'll have in this inventory system?
What to use instead of cling film to wrap pastry
Does XQuartz work on macOS Mojave?
Are the Night's Watch still required?
Side effects of Initiation by a Guru?
What are the advantages of luxury car brands like Acura/Lexus over their sibling non-luxury brands Honda/Toyota?
Would a small hole in a Faraday cage drastically reduce its effectiveness at blocking interference?
How to deal with employer who keeps me at work after working hours
Adding command shortcuts to /bin
What do "Sech" and "Vich" mean in this sentence?
Why do people keep telling me that I am a bad photographer?
Has the Hulk always been able to talk?
If all diagonals are drawn in a regular polygon from a vertex, the angles formed in that vertex are equal
Install LibreOffice-Writer Only not LibreOffice whole package
Selecting elements from a list where the order is set by another list
How to order a list to match the order of another list?Creating sums of elements from a listGenerating an ordered list of pairs of elements from ordered listsGenerating a random sample of elements from a list that includes a subset of the listDeleteDuplicates Delete elements of a listMatching the order of a master list of lists from a random list of listshow to apply Alternatives command to delete cases from a list?Adding multiplied corresponding elements from two different listsDelete elements of a list that appear in another listRemoving zeros from a list of equationsSelecting elements satisfying a condition
$begingroup$
I have two lists, list A
and list B
. All of the elements of A
are members of B
. The order of B
is fixed but the order of A
is random. I need to arrange the elements in A
as given in B
.
Example let A
A=5,2,8
and B
is
B=9,2,6,8,5,1
I need to reorder A
to be:
A=2,8,5
I'm looking for a clever one-liner. This is similar to How to order a list to match the order of another list? but there the two lists have the same length. I tried some variations on adding some padding.
list-manipulation core-language
$endgroup$
add a comment |
$begingroup$
I have two lists, list A
and list B
. All of the elements of A
are members of B
. The order of B
is fixed but the order of A
is random. I need to arrange the elements in A
as given in B
.
Example let A
A=5,2,8
and B
is
B=9,2,6,8,5,1
I need to reorder A
to be:
A=2,8,5
I'm looking for a clever one-liner. This is similar to How to order a list to match the order of another list? but there the two lists have the same length. I tried some variations on adding some padding.
list-manipulation core-language
$endgroup$
add a comment |
$begingroup$
I have two lists, list A
and list B
. All of the elements of A
are members of B
. The order of B
is fixed but the order of A
is random. I need to arrange the elements in A
as given in B
.
Example let A
A=5,2,8
and B
is
B=9,2,6,8,5,1
I need to reorder A
to be:
A=2,8,5
I'm looking for a clever one-liner. This is similar to How to order a list to match the order of another list? but there the two lists have the same length. I tried some variations on adding some padding.
list-manipulation core-language
$endgroup$
I have two lists, list A
and list B
. All of the elements of A
are members of B
. The order of B
is fixed but the order of A
is random. I need to arrange the elements in A
as given in B
.
Example let A
A=5,2,8
and B
is
B=9,2,6,8,5,1
I need to reorder A
to be:
A=2,8,5
I'm looking for a clever one-liner. This is similar to How to order a list to match the order of another list? but there the two lists have the same length. I tried some variations on adding some padding.
list-manipulation core-language
list-manipulation core-language
asked 4 hours ago
c186282c186282
833612
833612
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
SortBy[FirstPosition[#] @B&]@A
2, 8, 5
Also,
Cases[Alternatives @@ A] @ B
2, 8, 5
DeleteCases[Except[Alternatives @@ A]] @ B
2, 8, 5
$endgroup$
2
$begingroup$
Cases[B, Alternatives@@A]
is real-world poetry.
$endgroup$
– Roman
29 mins ago
$begingroup$
+1. Also:SortBy[A, PositionIndex[B]]
$endgroup$
– WReach
2 mins ago
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
);
);
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%2f197648%2fselecting-elements-from-a-list-where-the-order-is-set-by-another-list%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
$begingroup$
SortBy[FirstPosition[#] @B&]@A
2, 8, 5
Also,
Cases[Alternatives @@ A] @ B
2, 8, 5
DeleteCases[Except[Alternatives @@ A]] @ B
2, 8, 5
$endgroup$
2
$begingroup$
Cases[B, Alternatives@@A]
is real-world poetry.
$endgroup$
– Roman
29 mins ago
$begingroup$
+1. Also:SortBy[A, PositionIndex[B]]
$endgroup$
– WReach
2 mins ago
add a comment |
$begingroup$
SortBy[FirstPosition[#] @B&]@A
2, 8, 5
Also,
Cases[Alternatives @@ A] @ B
2, 8, 5
DeleteCases[Except[Alternatives @@ A]] @ B
2, 8, 5
$endgroup$
2
$begingroup$
Cases[B, Alternatives@@A]
is real-world poetry.
$endgroup$
– Roman
29 mins ago
$begingroup$
+1. Also:SortBy[A, PositionIndex[B]]
$endgroup$
– WReach
2 mins ago
add a comment |
$begingroup$
SortBy[FirstPosition[#] @B&]@A
2, 8, 5
Also,
Cases[Alternatives @@ A] @ B
2, 8, 5
DeleteCases[Except[Alternatives @@ A]] @ B
2, 8, 5
$endgroup$
SortBy[FirstPosition[#] @B&]@A
2, 8, 5
Also,
Cases[Alternatives @@ A] @ B
2, 8, 5
DeleteCases[Except[Alternatives @@ A]] @ B
2, 8, 5
edited 25 mins ago
answered 4 hours ago
kglrkglr
191k10210430
191k10210430
2
$begingroup$
Cases[B, Alternatives@@A]
is real-world poetry.
$endgroup$
– Roman
29 mins ago
$begingroup$
+1. Also:SortBy[A, PositionIndex[B]]
$endgroup$
– WReach
2 mins ago
add a comment |
2
$begingroup$
Cases[B, Alternatives@@A]
is real-world poetry.
$endgroup$
– Roman
29 mins ago
$begingroup$
+1. Also:SortBy[A, PositionIndex[B]]
$endgroup$
– WReach
2 mins ago
2
2
$begingroup$
Cases[B, Alternatives@@A]
is real-world poetry.$endgroup$
– Roman
29 mins ago
$begingroup$
Cases[B, Alternatives@@A]
is real-world poetry.$endgroup$
– Roman
29 mins ago
$begingroup$
+1. Also:
SortBy[A, PositionIndex[B]]
$endgroup$
– WReach
2 mins ago
$begingroup$
+1. Also:
SortBy[A, PositionIndex[B]]
$endgroup$
– WReach
2 mins ago
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.
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%2f197648%2fselecting-elements-from-a-list-where-the-order-is-set-by-another-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