Repeat elements in list, but the number of times each element is repeated is provided by a separate listComparing elements of the $n^textth$ sublist in a ragged list with the $n^textth$ member of a sequenceMaking a list of rules from the list of elementsHow to gather a list whitout no repeat element to nearest a certain numberSubtract second element of element of list from other list if the first elements are equalNumber elements in a listSelect first element in deepest list of each nested listHow to repeat each element in a list and the whole list as well?Adding a number to the 2nd element of each pair in a list of pairs
Did the Apollo missions fly "over the top" of the Van Allen radiation belts?
What is this nut?
Are monosubstituted cyclopropanes achiral or chiral?
Custom command to go to file whose name matches the current word under cursor?
Why didn't Philippine Airlines Flight 113 dump fuel prior to forced landing?
Does a Paladin with the Divine Health feature destroy a Green Slime?
Peaceable Bishops on an 10x10 grid
What mathematics activities get students physically moving?
How should I handle cases like this where the documentation for Bitcoin Core states one thing, and the actual program does something else?
DS 160 Have you traveled to any countries/regions within the last five years?
Variadic templates: unfold arguments in groups
What anti-aircraft magic adaptation would be better for dragons than flame spitting?
Is there no way in Windows 10 to type the Euro symbol € on a US English keyboard that has neither numeric keypad nor "alt gr" key?
Why would Earth be long-term unsuitable for an advanced alien species that's already colonized it?
Sue newspaper or the reporter for libel?
Pass arguments to the list of functions?
Best fighting style for a pacifist
Sump pump automated battery backup
Meaning of the term "vertex" in the topic of skeletal formulae
I peer reviewed a paper and found it to be sound - technically and language-wise. How should I write the review report?
My professor changed a take-home test to an in-class test with no notice. Can I fight the grade?
Is there any job security for tenured academics in Denmark?
Suggested Resources for Non-Linear Optimization
How to differentiate landing on top of an object from falling down the side?
Repeat elements in list, but the number of times each element is repeated is provided by a separate list
Comparing elements of the $n^textth$ sublist in a ragged list with the $n^textth$ member of a sequenceMaking a list of rules from the list of elementsHow to gather a list whitout no repeat element to nearest a certain numberSubtract second element of element of list from other list if the first elements are equalNumber elements in a listSelect first element in deepest list of each nested listHow to repeat each element in a list and the whole list as well?Adding a number to the 2nd element of each pair in a list of pairs
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
.everyonelovesstackoverflowposition:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;
$begingroup$
I'm trying to repeat each element of a list x
number of times, where x
is the corresponding element of the same position in another list.
For example, I have list A = 1,2,3,4
and another list B = 3,1,4,2
and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4
.
How do I get C
from A
and B
?
list-manipulation
$endgroup$
add a comment
|
$begingroup$
I'm trying to repeat each element of a list x
number of times, where x
is the corresponding element of the same position in another list.
For example, I have list A = 1,2,3,4
and another list B = 3,1,4,2
and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4
.
How do I get C
from A
and B
?
list-manipulation
$endgroup$
$begingroup$
What have you tried?
$endgroup$
– Edmund
Sep 18 at 16:23
add a comment
|
$begingroup$
I'm trying to repeat each element of a list x
number of times, where x
is the corresponding element of the same position in another list.
For example, I have list A = 1,2,3,4
and another list B = 3,1,4,2
and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4
.
How do I get C
from A
and B
?
list-manipulation
$endgroup$
I'm trying to repeat each element of a list x
number of times, where x
is the corresponding element of the same position in another list.
For example, I have list A = 1,2,3,4
and another list B = 3,1,4,2
and I'm trying to get C = 1,1,1,2,3,3,3,3,4,4
.
How do I get C
from A
and B
?
list-manipulation
list-manipulation
edited Sep 20 at 7:56
Alexey Popkov
48.1k4 gold badges118 silver badges282 bronze badges
48.1k4 gold badges118 silver badges282 bronze badges
asked Sep 18 at 16:12
reemodelsreemodels
2138 bronze badges
2138 bronze badges
$begingroup$
What have you tried?
$endgroup$
– Edmund
Sep 18 at 16:23
add a comment
|
$begingroup$
What have you tried?
$endgroup$
– Edmund
Sep 18 at 16:23
$begingroup$
What have you tried?
$endgroup$
– Edmund
Sep 18 at 16:23
$begingroup$
What have you tried?
$endgroup$
– Edmund
Sep 18 at 16:23
add a comment
|
5 Answers
5
active
oldest
votes
$begingroup$
Join @@ MapThread[Table, A,B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ Table @@@ Transpose @ A,B
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ MapThread[ConstantArray, A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Also
Internal`RepetitionFromMultiplicity @ Transpose[A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
kglr's proposals are nice, and the last (undocumented) one is very nice. As a variation, here is a solution using Inner[]
+ Flatten[]
:
Flatten[Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, List]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
As kglr notes, a shorter version is
Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
or in version 10 and later,
Inner[Table, 1, 2, 3, 4, 3, 1, 4, 2, Join]
$endgroup$
1
$begingroup$
shorterInner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
(+1)
$endgroup$
– kglr
Sep 19 at 14:10
$begingroup$
Indeed, thank you!
$endgroup$
– J. M. will be back soon♦
Sep 21 at 5:19
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Using ConstantArray
c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
or using Table
c = Flatten[Table[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
$endgroup$
add a comment
|
$begingroup$
Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.
f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]
Making this function Listable allows very simple calling method:
f[a, b] // Flatten
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
(* or *)
Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$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/4.0/"u003ecc by-sa 4.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%2f206438%2frepeat-elements-in-list-but-the-number-of-times-each-element-is-repeated-is-pro%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Join @@ MapThread[Table, A,B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ Table @@@ Transpose @ A,B
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ MapThread[ConstantArray, A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Also
Internal`RepetitionFromMultiplicity @ Transpose[A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
Join @@ MapThread[Table, A,B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ Table @@@ Transpose @ A,B
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ MapThread[ConstantArray, A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Also
Internal`RepetitionFromMultiplicity @ Transpose[A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
Join @@ MapThread[Table, A,B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ Table @@@ Transpose @ A,B
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ MapThread[ConstantArray, A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Also
Internal`RepetitionFromMultiplicity @ Transpose[A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
Join @@ MapThread[Table, A,B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ Table @@@ Transpose @ A,B
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Join @@ MapThread[ConstantArray, A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Also
Internal`RepetitionFromMultiplicity @ Transpose[A, B]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
edited Sep 18 at 20:24
answered Sep 18 at 17:05
kglrkglr
227k10 gold badges257 silver badges518 bronze badges
227k10 gold badges257 silver badges518 bronze badges
add a comment
|
add a comment
|
$begingroup$
kglr's proposals are nice, and the last (undocumented) one is very nice. As a variation, here is a solution using Inner[]
+ Flatten[]
:
Flatten[Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, List]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
As kglr notes, a shorter version is
Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
or in version 10 and later,
Inner[Table, 1, 2, 3, 4, 3, 1, 4, 2, Join]
$endgroup$
1
$begingroup$
shorterInner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
(+1)
$endgroup$
– kglr
Sep 19 at 14:10
$begingroup$
Indeed, thank you!
$endgroup$
– J. M. will be back soon♦
Sep 21 at 5:19
add a comment
|
$begingroup$
kglr's proposals are nice, and the last (undocumented) one is very nice. As a variation, here is a solution using Inner[]
+ Flatten[]
:
Flatten[Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, List]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
As kglr notes, a shorter version is
Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
or in version 10 and later,
Inner[Table, 1, 2, 3, 4, 3, 1, 4, 2, Join]
$endgroup$
1
$begingroup$
shorterInner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
(+1)
$endgroup$
– kglr
Sep 19 at 14:10
$begingroup$
Indeed, thank you!
$endgroup$
– J. M. will be back soon♦
Sep 21 at 5:19
add a comment
|
$begingroup$
kglr's proposals are nice, and the last (undocumented) one is very nice. As a variation, here is a solution using Inner[]
+ Flatten[]
:
Flatten[Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, List]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
As kglr notes, a shorter version is
Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
or in version 10 and later,
Inner[Table, 1, 2, 3, 4, 3, 1, 4, 2, Join]
$endgroup$
kglr's proposals are nice, and the last (undocumented) one is very nice. As a variation, here is a solution using Inner[]
+ Flatten[]
:
Flatten[Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, List]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
As kglr notes, a shorter version is
Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
or in version 10 and later,
Inner[Table, 1, 2, 3, 4, 3, 1, 4, 2, Join]
edited Sep 21 at 5:19
answered Sep 19 at 13:36
J. M. will be back soon♦J. M. will be back soon
104k11 gold badges322 silver badges484 bronze badges
104k11 gold badges322 silver badges484 bronze badges
1
$begingroup$
shorterInner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
(+1)
$endgroup$
– kglr
Sep 19 at 14:10
$begingroup$
Indeed, thank you!
$endgroup$
– J. M. will be back soon♦
Sep 21 at 5:19
add a comment
|
1
$begingroup$
shorterInner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
(+1)
$endgroup$
– kglr
Sep 19 at 14:10
$begingroup$
Indeed, thank you!
$endgroup$
– J. M. will be back soon♦
Sep 21 at 5:19
1
1
$begingroup$
shorter
Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
(+1)$endgroup$
– kglr
Sep 19 at 14:10
$begingroup$
shorter
Inner[ConstantArray, 1, 2, 3, 4, 3, 1, 4, 2, Join]
(+1)$endgroup$
– kglr
Sep 19 at 14:10
$begingroup$
Indeed, thank you!
$endgroup$
– J. M. will be back soon♦
Sep 21 at 5:19
$begingroup$
Indeed, thank you!
$endgroup$
– J. M. will be back soon♦
Sep 21 at 5:19
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Using ConstantArray
c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
or using Table
c = Flatten[Table[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
$endgroup$
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Using ConstantArray
c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
or using Table
c = Flatten[Table[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
$endgroup$
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Using ConstantArray
c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
or using Table
c = Flatten[Table[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
$endgroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Using ConstantArray
c = Flatten[ConstantArray[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
or using Table
c = Flatten[Table[#[[1]], #[[2]]] & /@
Transpose[a, b]]
(* 1, 1, 1, 2, 3, 3, 3, 3, 4, 4 *)
answered Sep 18 at 17:01
Bob HanlonBob Hanlon
69.6k3 gold badges38 silver badges107 bronze badges
69.6k3 gold badges38 silver badges107 bronze badges
add a comment
|
add a comment
|
$begingroup$
Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.
f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]
Making this function Listable allows very simple calling method:
f[a, b] // Flatten
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.
f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]
Making this function Listable allows very simple calling method:
f[a, b] // Flatten
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.
f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]
Making this function Listable allows very simple calling method:
f[a, b] // Flatten
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
Another way of approaching this is to define a function that carries out the basic task. In this case, to repeat the x element y times.
f[x_, y_] := ConstantArray[x, y]; SetAttributes[f, Listable]
Making this function Listable allows very simple calling method:
f[a, b] // Flatten
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
answered Sep 18 at 22:51
bill sbill s
57.9k3 gold badges81 silver badges162 bronze badges
57.9k3 gold badges81 silver badges162 bronze badges
add a comment
|
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
(* or *)
Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
(* or *)
Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
add a comment
|
$begingroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
(* or *)
Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
$endgroup$
a = 1, 2, 3, 4;
b = 3, 1, 4, 2;
Flatten[Table[a[[n]], n, Length[a], b[[n]]]]
(* or *)
Flatten[Table[Table[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
Flatten[Table[ConstantArray[a[[n]], b[[n]]], n, Length[a]]]
1, 1, 1, 2, 3, 3, 3, 3, 4, 4
answered Sep 18 at 22:45
MelaGoMelaGo
3,8361 gold badge3 silver badges12 bronze badges
3,8361 gold badge3 silver badges12 bronze badges
add a comment
|
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%2f206438%2frepeat-elements-in-list-but-the-number-of-times-each-element-is-repeated-is-pro%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
$begingroup$
What have you tried?
$endgroup$
– Edmund
Sep 18 at 16:23