Message Boards Message Boards

[✓] Generate a list with its elements in the order of a second list?

GROUPS:

This seems like it should be easy, but I've spend a half day trying unsuccessfully to solve it. I have three variables a,b,c (not in a list). The values are integers, For example a=6, b=21, c=7. I also have a list of the permuted numbers 1, 2 and 3. For example, let this list be be listA={3,1,2}. ListA could be any of the other five possible permutations of 1, 2 & 3, and I don't know what listA is ahead of time other than that it contains only a permutation of the numbers 1, 2 & 3..

So I want to form a new list containing a,b,and c in the order specified by the permutation list. Thus, the desired output would be listB={c,a,b}={7,6,21}. I've tried a lot of ways and nothing worked, probably because i was not using certain functions correctly, but maybe I just don't know which function to use.

I would appreciate any help you can give.

Kenneth Bures

POSTED BY: Ken Bures
Answer
26 days ago

Use Ordering to find the order of list, and apply it to the other one:

order = Ordering[list1]
list2 = list2[[order]]
POSTED BY: Sander Huisman
Answer
26 days ago

Use the function Part:

In[1]:= a = 6; b = 21; c = 7;

In[2]:= listA = {3, 1, 2};

In[3]:= Part[{a, b, c}, listA]

Out[3]= {7, 6, 21}

Shorthand:

In[4]:= {a, b, c}[[listA]]

Out[4]= {7, 6, 21}
POSTED BY: Hans Milton
Answer
26 days ago

I'm repeating my reply because somehow the formatting of my response got changed in the publishing, and it is hard to read.

Thank you for the two responses. The solution that uses the Ordering function does not seem to work. You can see from the code below that when I apply Ordering to list1, it changes the order from {3,1,2} t o {2,3,1). Then when I apply the Ordering to list2 I get {b,c,a} rather than {c,a,b}. Is it possible that I need to use Sort or SortBy along with Ordering? I haven't had a chance to play around with this.

list1 = {3, 1, 2} list2 = {a, b, c} order = Ordering[list1] list2 = list2[[order]]

{3, 1, 2}

{a, b, c}

{2, 3, 1}

{b, c, a}

The second solution uses Part, as in Part[list2,list1] or the shortcut list2[[list1]], and this simple solution does give the correct result.

Thanks again to both of you for taking the time to respond.

POSTED BY: Ken Bures
Answer
25 days ago

Can i place items in the list as unique and related to the list from data within each object in the list? Can I do an interpolated display 2d, 3d, movie?

POSTED BY: Rufus Warren
Answer
25 days ago

My "ordering list" is just a numerical list given the desired order of the other list. For example {3,1,2} to order {a,b,c} to give {c,a,b}. In that case the solution described by Hans Milton works. If you had other things besides numbers in that ordering list Mathematica might tell you that the Part expression cannot use those items. You'll have to try it for your specific case.

POSTED BY: Ken Bures
Answer
24 days ago

Any list can be permuted. In general, a list is still a list even if all the elements are not of the same type. An example:

sourceList = {ExampleData[{"TestImage", "House"}], Style[Red, 80], 77, Plot[Sin@x, {x, 0, 2*\[Pi]}]}

enter image description here

But as a special case a list that defines a permutation needs to have all its elements from a collection of consecutive integers, starting from the number 1. WL documentation:

permutationList = {2, 4, 1, 3};

The source list permuted:

sourceList[[permutationList]]

enter image description here

POSTED BY: Hans Milton
Answer
24 days ago

Welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST

The rules explain how to format your code properly. If you do not format code, it may become corrupted and useless to other members. Please EDIT your posts and make sure code blocks start on a new paragraph and look framed and colored like this.

int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

enter image description here

POSTED BY: Moderation Team
Answer
24 days ago

Group Abstract Group Abstract