Python equivalent of R's c()?
This question has already been asked here: Python equivalent of R c() function, but unfortunately the solution given doesn't exactly apply to me.
For instance, if I combine values, into a vector or list, in R like so:
x = c(2,3,6,8)
I can preform calculations on that list, for example:
x*2
Output:
4 6 12 16
However, I'm not sure how to achieve the same thing in Python. The previously asked question (in the link above) deals with a list of numbers in a particular range. for example,
x = list(range(1:10))
I am wondering, how do I define a list of numbers (not in a range) in Python?
python
add a comment |
This question has already been asked here: Python equivalent of R c() function, but unfortunately the solution given doesn't exactly apply to me.
For instance, if I combine values, into a vector or list, in R like so:
x = c(2,3,6,8)
I can preform calculations on that list, for example:
x*2
Output:
4 6 12 16
However, I'm not sure how to achieve the same thing in Python. The previously asked question (in the link above) deals with a list of numbers in a particular range. for example,
x = list(range(1:10))
I am wondering, how do I define a list of numbers (not in a range) in Python?
python
2
If you're usingnumpy
, then you just dox = np.array([2, 3, 6, 8])
thenx * 2
? eg - you actually give it alist
of numbers...
– Jon Clements♦
Nov 12 '18 at 16:44
(1) That does define a list -- it just creates it from a range; (2) if you want elementwise operations on vectors/matrices/n-dim arrays, usenumpy
.
– Linuxios
Nov 12 '18 at 16:45
np.array
did the job for me. Thanks for your help
– Electrino
Nov 12 '18 at 16:52
duplicate of stackoverflow.com/questions/32443778/…
– Jean-François Fabre
Nov 12 '18 at 19:47
add a comment |
This question has already been asked here: Python equivalent of R c() function, but unfortunately the solution given doesn't exactly apply to me.
For instance, if I combine values, into a vector or list, in R like so:
x = c(2,3,6,8)
I can preform calculations on that list, for example:
x*2
Output:
4 6 12 16
However, I'm not sure how to achieve the same thing in Python. The previously asked question (in the link above) deals with a list of numbers in a particular range. for example,
x = list(range(1:10))
I am wondering, how do I define a list of numbers (not in a range) in Python?
python
This question has already been asked here: Python equivalent of R c() function, but unfortunately the solution given doesn't exactly apply to me.
For instance, if I combine values, into a vector or list, in R like so:
x = c(2,3,6,8)
I can preform calculations on that list, for example:
x*2
Output:
4 6 12 16
However, I'm not sure how to achieve the same thing in Python. The previously asked question (in the link above) deals with a list of numbers in a particular range. for example,
x = list(range(1:10))
I am wondering, how do I define a list of numbers (not in a range) in Python?
python
python
asked Nov 12 '18 at 16:43
Electrino
591312
591312
2
If you're usingnumpy
, then you just dox = np.array([2, 3, 6, 8])
thenx * 2
? eg - you actually give it alist
of numbers...
– Jon Clements♦
Nov 12 '18 at 16:44
(1) That does define a list -- it just creates it from a range; (2) if you want elementwise operations on vectors/matrices/n-dim arrays, usenumpy
.
– Linuxios
Nov 12 '18 at 16:45
np.array
did the job for me. Thanks for your help
– Electrino
Nov 12 '18 at 16:52
duplicate of stackoverflow.com/questions/32443778/…
– Jean-François Fabre
Nov 12 '18 at 19:47
add a comment |
2
If you're usingnumpy
, then you just dox = np.array([2, 3, 6, 8])
thenx * 2
? eg - you actually give it alist
of numbers...
– Jon Clements♦
Nov 12 '18 at 16:44
(1) That does define a list -- it just creates it from a range; (2) if you want elementwise operations on vectors/matrices/n-dim arrays, usenumpy
.
– Linuxios
Nov 12 '18 at 16:45
np.array
did the job for me. Thanks for your help
– Electrino
Nov 12 '18 at 16:52
duplicate of stackoverflow.com/questions/32443778/…
– Jean-François Fabre
Nov 12 '18 at 19:47
2
2
If you're using
numpy
, then you just do x = np.array([2, 3, 6, 8])
then x * 2
? eg - you actually give it a list
of numbers...– Jon Clements♦
Nov 12 '18 at 16:44
If you're using
numpy
, then you just do x = np.array([2, 3, 6, 8])
then x * 2
? eg - you actually give it a list
of numbers...– Jon Clements♦
Nov 12 '18 at 16:44
(1) That does define a list -- it just creates it from a range; (2) if you want elementwise operations on vectors/matrices/n-dim arrays, use
numpy
.– Linuxios
Nov 12 '18 at 16:45
(1) That does define a list -- it just creates it from a range; (2) if you want elementwise operations on vectors/matrices/n-dim arrays, use
numpy
.– Linuxios
Nov 12 '18 at 16:45
np.array
did the job for me. Thanks for your help– Electrino
Nov 12 '18 at 16:52
np.array
did the job for me. Thanks for your help– Electrino
Nov 12 '18 at 16:52
duplicate of stackoverflow.com/questions/32443778/…
– Jean-François Fabre
Nov 12 '18 at 19:47
duplicate of stackoverflow.com/questions/32443778/…
– Jean-François Fabre
Nov 12 '18 at 19:47
add a comment |
1 Answer
1
active
oldest
votes
in python, you could use numpy arrays to do such things
import numpy
x = numpy.array([2, 3, 7])
y = x*2
and y will be equal to numpy.array([4, 6, 14])
I'm afraid not, that would produce:[2, 3, 7, 2, 3, 7]
– LeKhan9
Nov 12 '18 at 16:47
Python does that for a different purpose.
– Austin
Nov 12 '18 at 16:49
Thats my bad, I was thinking of numpy arrays, which should have just been my original answer...
– evinoshea
Nov 12 '18 at 17:31
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%2f53266549%2fpython-equivalent-of-rs-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
in python, you could use numpy arrays to do such things
import numpy
x = numpy.array([2, 3, 7])
y = x*2
and y will be equal to numpy.array([4, 6, 14])
I'm afraid not, that would produce:[2, 3, 7, 2, 3, 7]
– LeKhan9
Nov 12 '18 at 16:47
Python does that for a different purpose.
– Austin
Nov 12 '18 at 16:49
Thats my bad, I was thinking of numpy arrays, which should have just been my original answer...
– evinoshea
Nov 12 '18 at 17:31
add a comment |
in python, you could use numpy arrays to do such things
import numpy
x = numpy.array([2, 3, 7])
y = x*2
and y will be equal to numpy.array([4, 6, 14])
I'm afraid not, that would produce:[2, 3, 7, 2, 3, 7]
– LeKhan9
Nov 12 '18 at 16:47
Python does that for a different purpose.
– Austin
Nov 12 '18 at 16:49
Thats my bad, I was thinking of numpy arrays, which should have just been my original answer...
– evinoshea
Nov 12 '18 at 17:31
add a comment |
in python, you could use numpy arrays to do such things
import numpy
x = numpy.array([2, 3, 7])
y = x*2
and y will be equal to numpy.array([4, 6, 14])
in python, you could use numpy arrays to do such things
import numpy
x = numpy.array([2, 3, 7])
y = x*2
and y will be equal to numpy.array([4, 6, 14])
edited Nov 12 '18 at 17:30
answered Nov 12 '18 at 16:46
evinoshea
224
224
I'm afraid not, that would produce:[2, 3, 7, 2, 3, 7]
– LeKhan9
Nov 12 '18 at 16:47
Python does that for a different purpose.
– Austin
Nov 12 '18 at 16:49
Thats my bad, I was thinking of numpy arrays, which should have just been my original answer...
– evinoshea
Nov 12 '18 at 17:31
add a comment |
I'm afraid not, that would produce:[2, 3, 7, 2, 3, 7]
– LeKhan9
Nov 12 '18 at 16:47
Python does that for a different purpose.
– Austin
Nov 12 '18 at 16:49
Thats my bad, I was thinking of numpy arrays, which should have just been my original answer...
– evinoshea
Nov 12 '18 at 17:31
I'm afraid not, that would produce:
[2, 3, 7, 2, 3, 7]
– LeKhan9
Nov 12 '18 at 16:47
I'm afraid not, that would produce:
[2, 3, 7, 2, 3, 7]
– LeKhan9
Nov 12 '18 at 16:47
Python does that for a different purpose.
– Austin
Nov 12 '18 at 16:49
Python does that for a different purpose.
– Austin
Nov 12 '18 at 16:49
Thats my bad, I was thinking of numpy arrays, which should have just been my original answer...
– evinoshea
Nov 12 '18 at 17:31
Thats my bad, I was thinking of numpy arrays, which should have just been my original answer...
– evinoshea
Nov 12 '18 at 17:31
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53266549%2fpython-equivalent-of-rs-c%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
2
If you're using
numpy
, then you just dox = np.array([2, 3, 6, 8])
thenx * 2
? eg - you actually give it alist
of numbers...– Jon Clements♦
Nov 12 '18 at 16:44
(1) That does define a list -- it just creates it from a range; (2) if you want elementwise operations on vectors/matrices/n-dim arrays, use
numpy
.– Linuxios
Nov 12 '18 at 16:45
np.array
did the job for me. Thanks for your help– Electrino
Nov 12 '18 at 16:52
duplicate of stackoverflow.com/questions/32443778/…
– Jean-François Fabre
Nov 12 '18 at 19:47