I have a list of values, e.g., {1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5}
. I delete the duplicates with
DeleteDuplicates[{1, 1, 1 ,2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5}];
{1, 2, 3, 4, 5}
I want to perform some calculations on each value and at the end reverse the process of deleting.
From Tally
I know, how often the elements appear:
Tally[{1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5}];
{{1, 3}, {2, 2}, {3, 2}, {4, 5}, {5, 1}}
Now, from the calculations I have the new list {12, 14, 15, 16, 17}
. I want to reverse the process of DeleteDuplicates
on this list. Means: {{12,3},{14,2},{15,2},{16,5},{17,1}}
-> So, that I get:
{12, 12, 12, 14, 14, 15, 15, 16, 16, 16, 16, 17}
.
I want to do that, because the calculations take very long and I want to calculate duplicates two times.
Answer
tal = {{1, 3}, {2, 2}, {3, 2}, {4, 5}, {5, 1}};
lis = {12, 14, 15, 16, 17};
Flatten @ MapThread[Table[#1, {#2}] &, {lis, Last /@ tal}]
{12, 12, 12, 14, 14, 15, 15, 16, 16, 16, 16, 16, 17}
Or
Flatten[ConstantArray @@@ Transpose[{lis, Last /@ tal}]]
Comments
Post a Comment