Repeating array with transformation
How can I repeat the following example sequence:
l = np.array([3,4,5,6,7])
Up to n
times, doubling the values on each repetition. So for n=3
:
[3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28]
Is there a simple way avoiding loops with numpy
perhaps?
python arrays list numpy
add a comment |
How can I repeat the following example sequence:
l = np.array([3,4,5,6,7])
Up to n
times, doubling the values on each repetition. So for n=3
:
[3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28]
Is there a simple way avoiding loops with numpy
perhaps?
python arrays list numpy
1
Loops would be your best bet, is there a reason you cannot use loops?
– Zander
Dec 20 '18 at 17:07
add a comment |
How can I repeat the following example sequence:
l = np.array([3,4,5,6,7])
Up to n
times, doubling the values on each repetition. So for n=3
:
[3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28]
Is there a simple way avoiding loops with numpy
perhaps?
python arrays list numpy
How can I repeat the following example sequence:
l = np.array([3,4,5,6,7])
Up to n
times, doubling the values on each repetition. So for n=3
:
[3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28]
Is there a simple way avoiding loops with numpy
perhaps?
python arrays list numpy
python arrays list numpy
edited Dec 20 '18 at 17:15
jpp
102k2165116
102k2165116
asked Dec 20 '18 at 17:04
yatuyatu
14.6k41542
14.6k41542
1
Loops would be your best bet, is there a reason you cannot use loops?
– Zander
Dec 20 '18 at 17:07
add a comment |
1
Loops would be your best bet, is there a reason you cannot use loops?
– Zander
Dec 20 '18 at 17:07
1
1
Loops would be your best bet, is there a reason you cannot use loops?
– Zander
Dec 20 '18 at 17:07
Loops would be your best bet, is there a reason you cannot use loops?
– Zander
Dec 20 '18 at 17:07
add a comment |
7 Answers
7
active
oldest
votes
numpy.outer
+ numpy.ndarray.ravel
:
>>> a = np.array([3,4,5,6,7])
>>> n = 3
>>> factors = 2**np.arange(n)
>>> np.outer(factors, a).ravel()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
Details:
>>> factors
array([1, 2, 4])
>>> np.outer(factors, a)
array([[ 3, 4, 5, 6, 7], # 1*a
[ 6, 8, 10, 12, 14], # 2*a
[12, 16, 20, 24, 28]]) # 4*a
1
Very nice solution, didn't know aboutnp.outer
, thanks!
– yatu
Dec 20 '18 at 17:22
add a comment |
You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3
is the number of repetitions you need.
l = np.array([3,4,5,6,7])
final = np.concatenate([l*2**(i) for i in range(3)])
print (final)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
Here is one way:
>>> (l * [[1], [2], [4]]).flatten()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
(Only works if you want multiples of the input.)
add a comment |
You could do:
import numpy as np
l = np.array([3, 4, 5, 6, 7])
rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
print(rows.flatten())
Output
[ 3 4 5 6 7 6 8 10 12 14 12 16 20 24 28]
1
Nice one :) thx @Danielmesejo
– yatu
Dec 20 '18 at 17:12
add a comment |
You can use np.power
with np.ndarray.ravel
:
A = np.array([3,4,5,6,7])
res = (A * np.power(2, np.arange(3))[:, None]).ravel()
print(res)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can to loop over n
, and concatenate a new list each time:
l = [3,4,5,6,7]
n = 3
new_l =
for i in range(n):
new_l += [j*2**i for j in l]
new_l = np.array(new_l)
Output
array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can do that in pure python using list comprehensions:
l = [3,4,5,6,7]
n = 3
mult = [1 * 2**i for i in range(n)]
final = list([x*m for m in mult for x in l])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fstackoverflow.com%2fquestions%2f53873120%2frepeating-array-with-transformation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
numpy.outer
+ numpy.ndarray.ravel
:
>>> a = np.array([3,4,5,6,7])
>>> n = 3
>>> factors = 2**np.arange(n)
>>> np.outer(factors, a).ravel()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
Details:
>>> factors
array([1, 2, 4])
>>> np.outer(factors, a)
array([[ 3, 4, 5, 6, 7], # 1*a
[ 6, 8, 10, 12, 14], # 2*a
[12, 16, 20, 24, 28]]) # 4*a
1
Very nice solution, didn't know aboutnp.outer
, thanks!
– yatu
Dec 20 '18 at 17:22
add a comment |
numpy.outer
+ numpy.ndarray.ravel
:
>>> a = np.array([3,4,5,6,7])
>>> n = 3
>>> factors = 2**np.arange(n)
>>> np.outer(factors, a).ravel()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
Details:
>>> factors
array([1, 2, 4])
>>> np.outer(factors, a)
array([[ 3, 4, 5, 6, 7], # 1*a
[ 6, 8, 10, 12, 14], # 2*a
[12, 16, 20, 24, 28]]) # 4*a
1
Very nice solution, didn't know aboutnp.outer
, thanks!
– yatu
Dec 20 '18 at 17:22
add a comment |
numpy.outer
+ numpy.ndarray.ravel
:
>>> a = np.array([3,4,5,6,7])
>>> n = 3
>>> factors = 2**np.arange(n)
>>> np.outer(factors, a).ravel()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
Details:
>>> factors
array([1, 2, 4])
>>> np.outer(factors, a)
array([[ 3, 4, 5, 6, 7], # 1*a
[ 6, 8, 10, 12, 14], # 2*a
[12, 16, 20, 24, 28]]) # 4*a
numpy.outer
+ numpy.ndarray.ravel
:
>>> a = np.array([3,4,5,6,7])
>>> n = 3
>>> factors = 2**np.arange(n)
>>> np.outer(factors, a).ravel()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
Details:
>>> factors
array([1, 2, 4])
>>> np.outer(factors, a)
array([[ 3, 4, 5, 6, 7], # 1*a
[ 6, 8, 10, 12, 14], # 2*a
[12, 16, 20, 24, 28]]) # 4*a
answered Dec 20 '18 at 17:14
timgebtimgeb
51.3k126794
51.3k126794
1
Very nice solution, didn't know aboutnp.outer
, thanks!
– yatu
Dec 20 '18 at 17:22
add a comment |
1
Very nice solution, didn't know aboutnp.outer
, thanks!
– yatu
Dec 20 '18 at 17:22
1
1
Very nice solution, didn't know about
np.outer
, thanks!– yatu
Dec 20 '18 at 17:22
Very nice solution, didn't know about
np.outer
, thanks!– yatu
Dec 20 '18 at 17:22
add a comment |
You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3
is the number of repetitions you need.
l = np.array([3,4,5,6,7])
final = np.concatenate([l*2**(i) for i in range(3)])
print (final)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3
is the number of repetitions you need.
l = np.array([3,4,5,6,7])
final = np.concatenate([l*2**(i) for i in range(3)])
print (final)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3
is the number of repetitions you need.
l = np.array([3,4,5,6,7])
final = np.concatenate([l*2**(i) for i in range(3)])
print (final)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
You can use concatenate and list comprehension using powers of 2 as the multiplicative factor. Here 3
is the number of repetitions you need.
l = np.array([3,4,5,6,7])
final = np.concatenate([l*2**(i) for i in range(3)])
print (final)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
answered Dec 20 '18 at 17:10
BazingaaBazingaa
14.8k21230
14.8k21230
add a comment |
add a comment |
Here is one way:
>>> (l * [[1], [2], [4]]).flatten()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
(Only works if you want multiples of the input.)
add a comment |
Here is one way:
>>> (l * [[1], [2], [4]]).flatten()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
(Only works if you want multiples of the input.)
add a comment |
Here is one way:
>>> (l * [[1], [2], [4]]).flatten()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
(Only works if you want multiples of the input.)
Here is one way:
>>> (l * [[1], [2], [4]]).flatten()
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
(Only works if you want multiples of the input.)
answered Dec 20 '18 at 17:10
NPENPE
357k67759890
357k67759890
add a comment |
add a comment |
You could do:
import numpy as np
l = np.array([3, 4, 5, 6, 7])
rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
print(rows.flatten())
Output
[ 3 4 5 6 7 6 8 10 12 14 12 16 20 24 28]
1
Nice one :) thx @Danielmesejo
– yatu
Dec 20 '18 at 17:12
add a comment |
You could do:
import numpy as np
l = np.array([3, 4, 5, 6, 7])
rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
print(rows.flatten())
Output
[ 3 4 5 6 7 6 8 10 12 14 12 16 20 24 28]
1
Nice one :) thx @Danielmesejo
– yatu
Dec 20 '18 at 17:12
add a comment |
You could do:
import numpy as np
l = np.array([3, 4, 5, 6, 7])
rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
print(rows.flatten())
Output
[ 3 4 5 6 7 6 8 10 12 14 12 16 20 24 28]
You could do:
import numpy as np
l = np.array([3, 4, 5, 6, 7])
rows = np.tile(l, 3).reshape(-1, len(l)) * np.power(2, np.arange(3)).reshape(-1, 1)
print(rows.flatten())
Output
[ 3 4 5 6 7 6 8 10 12 14 12 16 20 24 28]
answered Dec 20 '18 at 17:11
Daniel MesejoDaniel Mesejo
18.7k21533
18.7k21533
1
Nice one :) thx @Danielmesejo
– yatu
Dec 20 '18 at 17:12
add a comment |
1
Nice one :) thx @Danielmesejo
– yatu
Dec 20 '18 at 17:12
1
1
Nice one :) thx @Danielmesejo
– yatu
Dec 20 '18 at 17:12
Nice one :) thx @Danielmesejo
– yatu
Dec 20 '18 at 17:12
add a comment |
You can use np.power
with np.ndarray.ravel
:
A = np.array([3,4,5,6,7])
res = (A * np.power(2, np.arange(3))[:, None]).ravel()
print(res)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can use np.power
with np.ndarray.ravel
:
A = np.array([3,4,5,6,7])
res = (A * np.power(2, np.arange(3))[:, None]).ravel()
print(res)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can use np.power
with np.ndarray.ravel
:
A = np.array([3,4,5,6,7])
res = (A * np.power(2, np.arange(3))[:, None]).ravel()
print(res)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
You can use np.power
with np.ndarray.ravel
:
A = np.array([3,4,5,6,7])
res = (A * np.power(2, np.arange(3))[:, None]).ravel()
print(res)
array([ 3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
answered Dec 20 '18 at 17:13
jppjpp
102k2165116
102k2165116
add a comment |
add a comment |
You can to loop over n
, and concatenate a new list each time:
l = [3,4,5,6,7]
n = 3
new_l =
for i in range(n):
new_l += [j*2**i for j in l]
new_l = np.array(new_l)
Output
array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can to loop over n
, and concatenate a new list each time:
l = [3,4,5,6,7]
n = 3
new_l =
for i in range(n):
new_l += [j*2**i for j in l]
new_l = np.array(new_l)
Output
array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
add a comment |
You can to loop over n
, and concatenate a new list each time:
l = [3,4,5,6,7]
n = 3
new_l =
for i in range(n):
new_l += [j*2**i for j in l]
new_l = np.array(new_l)
Output
array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
You can to loop over n
, and concatenate a new list each time:
l = [3,4,5,6,7]
n = 3
new_l =
for i in range(n):
new_l += [j*2**i for j in l]
new_l = np.array(new_l)
Output
array([3, 4, 5, 6, 7, 6, 8, 10, 12, 14, 12, 16, 20, 24, 28])
answered Dec 20 '18 at 17:08
TimTim
1,790621
1,790621
add a comment |
add a comment |
You can do that in pure python using list comprehensions:
l = [3,4,5,6,7]
n = 3
mult = [1 * 2**i for i in range(n)]
final = list([x*m for m in mult for x in l])
add a comment |
You can do that in pure python using list comprehensions:
l = [3,4,5,6,7]
n = 3
mult = [1 * 2**i for i in range(n)]
final = list([x*m for m in mult for x in l])
add a comment |
You can do that in pure python using list comprehensions:
l = [3,4,5,6,7]
n = 3
mult = [1 * 2**i for i in range(n)]
final = list([x*m for m in mult for x in l])
You can do that in pure python using list comprehensions:
l = [3,4,5,6,7]
n = 3
mult = [1 * 2**i for i in range(n)]
final = list([x*m for m in mult for x in l])
answered Dec 27 '18 at 10:08
feebarsceviciusfeebarscevicius
50437
50437
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53873120%2frepeating-array-with-transformation%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
1
Loops would be your best bet, is there a reason you cannot use loops?
– Zander
Dec 20 '18 at 17:07