Merging two arrays into a single array, to be plugged into a function
I have a way I would like to approach my problem, however am struggling with the syntax of the problem.
I have a function, lets call it f(a,b)
.
I also have an np.array
of complex numbers for a and b.
Eg, a = [1+j, 2+j.....]
, and b = [3+2j, 4+4j....]
.
What I would like to do is combine these two arrays into an array called c, of the form c = [(1+j,3+2j),(2+j,4+4j)….]
, where the ith value of c = [(ai,bi)]
.
I feel I should then plug these ith values of c into my function f(a,b) in a loop (hopefully python can convert ith components of array into two arguments), which would then produce an array of the ith values of f.
I feel this method should work, however the main issue I'm having is syntactical. Does anyone know of any inbuilt functions which could merge two arrays in such a way?
First post on this so any replies would be appreciated! Thanks
python arrays list loops numpy
add a comment |
I have a way I would like to approach my problem, however am struggling with the syntax of the problem.
I have a function, lets call it f(a,b)
.
I also have an np.array
of complex numbers for a and b.
Eg, a = [1+j, 2+j.....]
, and b = [3+2j, 4+4j....]
.
What I would like to do is combine these two arrays into an array called c, of the form c = [(1+j,3+2j),(2+j,4+4j)….]
, where the ith value of c = [(ai,bi)]
.
I feel I should then plug these ith values of c into my function f(a,b) in a loop (hopefully python can convert ith components of array into two arguments), which would then produce an array of the ith values of f.
I feel this method should work, however the main issue I'm having is syntactical. Does anyone know of any inbuilt functions which could merge two arrays in such a way?
First post on this so any replies would be appreciated! Thanks
python arrays list loops numpy
Thezip
function should be useful. Tryc = list(zip(a, b))
– doodhwala
Nov 12 '18 at 15:59
c = np.stack((a,b), axis=1)
can create a (n,2) array, but given your description off
I'm not sure it will help. Sounds likef
takes 2 positional scalar arguments,a
andb
, and does some sort of math returning another number. Or can it work with a 2d array? Or even with a tuple?
– hpaulj
Nov 12 '18 at 17:01
I think you need to give an actual demof(a,b)
. It doesn't have to be exactly the one you'll need, but it should behave just the same with respect to inputs (type and size, etc).
– hpaulj
Nov 12 '18 at 18:31
I will be integrating f using scipy.integrate.simpson. I’ve simplified my function a lot here, but ultimately I’m aiming to produce and array of f(a,b) and then an array of b’s , since the b parameter is the integration variable . I believe scipy.integrate.simpson requires an array of f(a,b) and an array of , in this case, b values to be inputted in to it. So now you see why I’m a bit stuck on getting the syntax correct to produce an array of f(a,b) for different values of a and b in a set range.
– Pox 219
Nov 12 '18 at 19:06
add a comment |
I have a way I would like to approach my problem, however am struggling with the syntax of the problem.
I have a function, lets call it f(a,b)
.
I also have an np.array
of complex numbers for a and b.
Eg, a = [1+j, 2+j.....]
, and b = [3+2j, 4+4j....]
.
What I would like to do is combine these two arrays into an array called c, of the form c = [(1+j,3+2j),(2+j,4+4j)….]
, where the ith value of c = [(ai,bi)]
.
I feel I should then plug these ith values of c into my function f(a,b) in a loop (hopefully python can convert ith components of array into two arguments), which would then produce an array of the ith values of f.
I feel this method should work, however the main issue I'm having is syntactical. Does anyone know of any inbuilt functions which could merge two arrays in such a way?
First post on this so any replies would be appreciated! Thanks
python arrays list loops numpy
I have a way I would like to approach my problem, however am struggling with the syntax of the problem.
I have a function, lets call it f(a,b)
.
I also have an np.array
of complex numbers for a and b.
Eg, a = [1+j, 2+j.....]
, and b = [3+2j, 4+4j....]
.
What I would like to do is combine these two arrays into an array called c, of the form c = [(1+j,3+2j),(2+j,4+4j)….]
, where the ith value of c = [(ai,bi)]
.
I feel I should then plug these ith values of c into my function f(a,b) in a loop (hopefully python can convert ith components of array into two arguments), which would then produce an array of the ith values of f.
I feel this method should work, however the main issue I'm having is syntactical. Does anyone know of any inbuilt functions which could merge two arrays in such a way?
First post on this so any replies would be appreciated! Thanks
python arrays list loops numpy
python arrays list loops numpy
edited Nov 12 '18 at 17:56
Woody1193
2,246930
2,246930
asked Nov 12 '18 at 15:57
Pox 219
1
1
Thezip
function should be useful. Tryc = list(zip(a, b))
– doodhwala
Nov 12 '18 at 15:59
c = np.stack((a,b), axis=1)
can create a (n,2) array, but given your description off
I'm not sure it will help. Sounds likef
takes 2 positional scalar arguments,a
andb
, and does some sort of math returning another number. Or can it work with a 2d array? Or even with a tuple?
– hpaulj
Nov 12 '18 at 17:01
I think you need to give an actual demof(a,b)
. It doesn't have to be exactly the one you'll need, but it should behave just the same with respect to inputs (type and size, etc).
– hpaulj
Nov 12 '18 at 18:31
I will be integrating f using scipy.integrate.simpson. I’ve simplified my function a lot here, but ultimately I’m aiming to produce and array of f(a,b) and then an array of b’s , since the b parameter is the integration variable . I believe scipy.integrate.simpson requires an array of f(a,b) and an array of , in this case, b values to be inputted in to it. So now you see why I’m a bit stuck on getting the syntax correct to produce an array of f(a,b) for different values of a and b in a set range.
– Pox 219
Nov 12 '18 at 19:06
add a comment |
Thezip
function should be useful. Tryc = list(zip(a, b))
– doodhwala
Nov 12 '18 at 15:59
c = np.stack((a,b), axis=1)
can create a (n,2) array, but given your description off
I'm not sure it will help. Sounds likef
takes 2 positional scalar arguments,a
andb
, and does some sort of math returning another number. Or can it work with a 2d array? Or even with a tuple?
– hpaulj
Nov 12 '18 at 17:01
I think you need to give an actual demof(a,b)
. It doesn't have to be exactly the one you'll need, but it should behave just the same with respect to inputs (type and size, etc).
– hpaulj
Nov 12 '18 at 18:31
I will be integrating f using scipy.integrate.simpson. I’ve simplified my function a lot here, but ultimately I’m aiming to produce and array of f(a,b) and then an array of b’s , since the b parameter is the integration variable . I believe scipy.integrate.simpson requires an array of f(a,b) and an array of , in this case, b values to be inputted in to it. So now you see why I’m a bit stuck on getting the syntax correct to produce an array of f(a,b) for different values of a and b in a set range.
– Pox 219
Nov 12 '18 at 19:06
The
zip
function should be useful. Try c = list(zip(a, b))
– doodhwala
Nov 12 '18 at 15:59
The
zip
function should be useful. Try c = list(zip(a, b))
– doodhwala
Nov 12 '18 at 15:59
c = np.stack((a,b), axis=1)
can create a (n,2) array, but given your description of f
I'm not sure it will help. Sounds like f
takes 2 positional scalar arguments, a
and b
, and does some sort of math returning another number. Or can it work with a 2d array? Or even with a tuple?– hpaulj
Nov 12 '18 at 17:01
c = np.stack((a,b), axis=1)
can create a (n,2) array, but given your description of f
I'm not sure it will help. Sounds like f
takes 2 positional scalar arguments, a
and b
, and does some sort of math returning another number. Or can it work with a 2d array? Or even with a tuple?– hpaulj
Nov 12 '18 at 17:01
I think you need to give an actual demo
f(a,b)
. It doesn't have to be exactly the one you'll need, but it should behave just the same with respect to inputs (type and size, etc).– hpaulj
Nov 12 '18 at 18:31
I think you need to give an actual demo
f(a,b)
. It doesn't have to be exactly the one you'll need, but it should behave just the same with respect to inputs (type and size, etc).– hpaulj
Nov 12 '18 at 18:31
I will be integrating f using scipy.integrate.simpson. I’ve simplified my function a lot here, but ultimately I’m aiming to produce and array of f(a,b) and then an array of b’s , since the b parameter is the integration variable . I believe scipy.integrate.simpson requires an array of f(a,b) and an array of , in this case, b values to be inputted in to it. So now you see why I’m a bit stuck on getting the syntax correct to produce an array of f(a,b) for different values of a and b in a set range.
– Pox 219
Nov 12 '18 at 19:06
I will be integrating f using scipy.integrate.simpson. I’ve simplified my function a lot here, but ultimately I’m aiming to produce and array of f(a,b) and then an array of b’s , since the b parameter is the integration variable . I believe scipy.integrate.simpson requires an array of f(a,b) and an array of , in this case, b values to be inputted in to it. So now you see why I’m a bit stuck on getting the syntax correct to produce an array of f(a,b) for different values of a and b in a set range.
– Pox 219
Nov 12 '18 at 19:06
add a comment |
2 Answers
2
active
oldest
votes
Use a list comprehension:
c = [ci for ci in zip(a, b)]
zip will return a iterator over tuples containing one element from a
and b
.
With more verbosity, this is:
c = [(ai, bi) for (ai, bi) in zip(a, b)]
add a comment |
You just need to use zip :
c = zip(a, b)
a = ['x', 'y', 'z']
b = [1, 2, 3]
c = zip(a, b)
c[1]
>>> ('y', 2)
Edit : added the example
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%2f53265780%2fmerging-two-arrays-into-a-single-array-to-be-plugged-into-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use a list comprehension:
c = [ci for ci in zip(a, b)]
zip will return a iterator over tuples containing one element from a
and b
.
With more verbosity, this is:
c = [(ai, bi) for (ai, bi) in zip(a, b)]
add a comment |
Use a list comprehension:
c = [ci for ci in zip(a, b)]
zip will return a iterator over tuples containing one element from a
and b
.
With more verbosity, this is:
c = [(ai, bi) for (ai, bi) in zip(a, b)]
add a comment |
Use a list comprehension:
c = [ci for ci in zip(a, b)]
zip will return a iterator over tuples containing one element from a
and b
.
With more verbosity, this is:
c = [(ai, bi) for (ai, bi) in zip(a, b)]
Use a list comprehension:
c = [ci for ci in zip(a, b)]
zip will return a iterator over tuples containing one element from a
and b
.
With more verbosity, this is:
c = [(ai, bi) for (ai, bi) in zip(a, b)]
answered Nov 12 '18 at 15:58
Matthieu Brucher
12.6k22140
12.6k22140
add a comment |
add a comment |
You just need to use zip :
c = zip(a, b)
a = ['x', 'y', 'z']
b = [1, 2, 3]
c = zip(a, b)
c[1]
>>> ('y', 2)
Edit : added the example
add a comment |
You just need to use zip :
c = zip(a, b)
a = ['x', 'y', 'z']
b = [1, 2, 3]
c = zip(a, b)
c[1]
>>> ('y', 2)
Edit : added the example
add a comment |
You just need to use zip :
c = zip(a, b)
a = ['x', 'y', 'z']
b = [1, 2, 3]
c = zip(a, b)
c[1]
>>> ('y', 2)
Edit : added the example
You just need to use zip :
c = zip(a, b)
a = ['x', 'y', 'z']
b = [1, 2, 3]
c = zip(a, b)
c[1]
>>> ('y', 2)
Edit : added the example
answered Nov 12 '18 at 16:06
Q-life
666
666
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.
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%2f53265780%2fmerging-two-arrays-into-a-single-array-to-be-plugged-into-a-function%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
The
zip
function should be useful. Tryc = list(zip(a, b))
– doodhwala
Nov 12 '18 at 15:59
c = np.stack((a,b), axis=1)
can create a (n,2) array, but given your description off
I'm not sure it will help. Sounds likef
takes 2 positional scalar arguments,a
andb
, and does some sort of math returning another number. Or can it work with a 2d array? Or even with a tuple?– hpaulj
Nov 12 '18 at 17:01
I think you need to give an actual demo
f(a,b)
. It doesn't have to be exactly the one you'll need, but it should behave just the same with respect to inputs (type and size, etc).– hpaulj
Nov 12 '18 at 18:31
I will be integrating f using scipy.integrate.simpson. I’ve simplified my function a lot here, but ultimately I’m aiming to produce and array of f(a,b) and then an array of b’s , since the b parameter is the integration variable . I believe scipy.integrate.simpson requires an array of f(a,b) and an array of , in this case, b values to be inputted in to it. So now you see why I’m a bit stuck on getting the syntax correct to produce an array of f(a,b) for different values of a and b in a set range.
– Pox 219
Nov 12 '18 at 19:06