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 numberChanging the order of elements in a listSubtract 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?
Why NASA publish all the results/data it gets?
Wired to Wireless Doorbell
Gas leaking in base of new gas range?
As an employer, can I compel my employees to vote?
Where are they calling from?
Resolving moral conflict
Nanomachines exist that enable Axolotl-levels of regeneration - So how can crippling injuries exist as well?
Is it true that, "just ten trading days represent 63 per cent of the returns of the past 50 years"?
Where Does VDD+0.3V Input Limit Come From on IC chips?
What can a pilot do if an air traffic controller is incapacitated?
The 100 soldier problem
CDG baggage claim before or after immigration?
How to deal with my team leader who keeps calling me about project updates even though I am on leave for personal reasons?
How would a native speaker correct themselves when they misspeak?
Do liquid propellant rocket engines experience thrust oscillation?
Can planetary bodies have a second axis of rotation?
How to make interviewee comfortable interviewing in lounge chairs
Escape the labyrinth!
Manager encourages me to take day of sick leave instead of PTO, what's in it for him?
Leaving a job that I just took based on false promise of a raise. What do I tell future interviewers?
GitHub repo with Apache License version 2 in package.json, but no full license copy nor comment headers
I reverse the source code, you negate the output!
How does one calculate the distribution of the Matt Colville way of rolling stats?
How do rulers get rich from war?
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 numberChanging the order of elements in a listSubtract 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?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$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?
Thanks.
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?
Thanks.
list-manipulation
$endgroup$
$begingroup$
What have you tried?
$endgroup$
– Edmund
8 hours ago
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?
Thanks.
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?
Thanks.
list-manipulation
list-manipulation
asked 8 hours ago
JinJin
564 bronze badges
564 bronze badges
$begingroup$
What have you tried?
$endgroup$
– Edmund
8 hours ago
add a comment
|
$begingroup$
What have you tried?
$endgroup$
– Edmund
8 hours ago
$begingroup$
What have you tried?
$endgroup$
– Edmund
8 hours ago
$begingroup$
What have you tried?
$endgroup$
– Edmund
8 hours ago
add a comment
|
4 Answers
4
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$
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
4 Answers
4
active
oldest
votes
4 Answers
4
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 4 hours ago
answered 7 hours ago
kglrkglr
217k10 gold badges247 silver badges497 bronze badges
217k10 gold badges247 silver badges497 bronze badges
add a comment
|
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 7 hours ago
Bob HanlonBob Hanlon
65.6k3 gold badges37 silver badges100 bronze badges
65.6k3 gold badges37 silver badges100 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 1 hour ago
bill sbill s
56.4k3 gold badges80 silver badges161 bronze badges
56.4k3 gold badges80 silver badges161 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 1 hour ago
MelaGoMelaGo
2,8111 gold badge2 silver badges8 bronze badges
2,8111 gold badge2 silver badges8 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
8 hours ago