How to concatenate arrays with different size in list or array?












0














I have multiple arrays with different size such as m0xn0xk0, m1xn1xk1, m2xn2xk2.... I am looking for a method to concatenate them into a single array A_concate that can be accessed in future based on the index. It likes



A_concate [0]=a_0 # size of m0xn0xk0
A_concate [1]=a_1 # size of m1xn1xk1
...


Do we have any way to do it in python? Thanks all










share|improve this question


















  • 1




    When you say array do you mean numpy arrays? Numpy arrays have to be rectangular so you can only put together blocks in a way that you end up with a multidimensional rectangle.
    – Andras Deak
    Nov 12 '18 at 15:16










  • Yes. There are numpy array
    – Moon Lee
    Nov 12 '18 at 15:20
















0














I have multiple arrays with different size such as m0xn0xk0, m1xn1xk1, m2xn2xk2.... I am looking for a method to concatenate them into a single array A_concate that can be accessed in future based on the index. It likes



A_concate [0]=a_0 # size of m0xn0xk0
A_concate [1]=a_1 # size of m1xn1xk1
...


Do we have any way to do it in python? Thanks all










share|improve this question


















  • 1




    When you say array do you mean numpy arrays? Numpy arrays have to be rectangular so you can only put together blocks in a way that you end up with a multidimensional rectangle.
    – Andras Deak
    Nov 12 '18 at 15:16










  • Yes. There are numpy array
    – Moon Lee
    Nov 12 '18 at 15:20














0












0








0







I have multiple arrays with different size such as m0xn0xk0, m1xn1xk1, m2xn2xk2.... I am looking for a method to concatenate them into a single array A_concate that can be accessed in future based on the index. It likes



A_concate [0]=a_0 # size of m0xn0xk0
A_concate [1]=a_1 # size of m1xn1xk1
...


Do we have any way to do it in python? Thanks all










share|improve this question













I have multiple arrays with different size such as m0xn0xk0, m1xn1xk1, m2xn2xk2.... I am looking for a method to concatenate them into a single array A_concate that can be accessed in future based on the index. It likes



A_concate [0]=a_0 # size of m0xn0xk0
A_concate [1]=a_1 # size of m1xn1xk1
...


Do we have any way to do it in python? Thanks all







python arrays python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 15:15









Moon Lee

83112




83112








  • 1




    When you say array do you mean numpy arrays? Numpy arrays have to be rectangular so you can only put together blocks in a way that you end up with a multidimensional rectangle.
    – Andras Deak
    Nov 12 '18 at 15:16










  • Yes. There are numpy array
    – Moon Lee
    Nov 12 '18 at 15:20














  • 1




    When you say array do you mean numpy arrays? Numpy arrays have to be rectangular so you can only put together blocks in a way that you end up with a multidimensional rectangle.
    – Andras Deak
    Nov 12 '18 at 15:16










  • Yes. There are numpy array
    – Moon Lee
    Nov 12 '18 at 15:20








1




1




When you say array do you mean numpy arrays? Numpy arrays have to be rectangular so you can only put together blocks in a way that you end up with a multidimensional rectangle.
– Andras Deak
Nov 12 '18 at 15:16




When you say array do you mean numpy arrays? Numpy arrays have to be rectangular so you can only put together blocks in a way that you end up with a multidimensional rectangle.
– Andras Deak
Nov 12 '18 at 15:16












Yes. There are numpy array
– Moon Lee
Nov 12 '18 at 15:20




Yes. There are numpy array
– Moon Lee
Nov 12 '18 at 15:20












2 Answers
2






active

oldest

votes


















1














It depends on what you want to do with this structure. For instance, if there is some value unique to m0xn0xk0, m1xn1xk1, m2xn2xk2... that you want to index by then you would use a dictionary like this:



A_concate = {}
A_concate[some_key] = m0xn0xk0
A_concate[some_other_key] = m1xn1xk1


and then you could access them using the key values. On the other hand, if you just want all these lists to live on the same object and you're only going to iterate over them, using a list is fine:



def listify(*args):
return list(args)

A_concate = listify(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


Finally, if you just want all the values of these objects in the same place, then you could just concatenate them into one list:



def flatten(*lists):
a =
for l in lists:
a.extend(l)
return a

A_concate = flatten(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


It just depends on what you want to do with it.






share|improve this answer





















  • Thanks. It worked.
    – Moon Lee
    Nov 12 '18 at 15:38










  • Hi. If I want to concatenate them in loop. How can I modify your code? I have write a script at here but it does not show correct dimension repl.it/repls/LightGrotesqueCodeview
    – Moon Lee
    Nov 12 '18 at 15:44










  • Could you look at my issue? It is missing one dims
    – Moon Lee
    Nov 12 '18 at 15:59










  • Of course it is. extend unpacks your list and adds each element to the list you're extending
    – Woody1193
    Nov 12 '18 at 16:08






  • 1




    @MoonLee Sorry about that, I've been doing a lot of C# lately so I got turned around. Use append instead :)
    – Woody1193
    Nov 12 '18 at 16:32



















1














If that is all you want to do then why not just put them in a list?



You could use



A_concate = [a_0, a_1, a_2,..., a_n]


If you really want to use an array you could use



import numpy as np
A_concate = np.array([a_0, a_1, a_2,..., a_n])


The above will give you an array of objects. Where each entry is an array. You won't have the full functionality to slice across the internal arrays but it depends on what you want to do



Here is a minimal example of the above



import numpy as np
a_0 = np.arange(9)
a_1 = np.arange(10)
a_2 = np.arange(16)
A_concate = np.array([a_0, a_1, a_2])


EDIT



Add another example to show it works more generally than for 1D arrays



import numpy as np
a_0 = np.random.normal(0,1,size=(3,4,5,))
a_1 = np.random.normal(0,1,size=(4,5,6,))
a_2 = np.random.normal(0,1,size=(35,19,97,))
a_3 = np.random.normal(0,1,size=(2,6,14,22,89))
A_concate = np.array([a_0, a_1, a_2, a_3])





share|improve this answer























  • Sorry. there arrays have different size. Could you show the example with size of 3x4x5 for a_0, 4x5x6 for a_1...
    – Moon Lee
    Nov 12 '18 at 15:33










  • Yeh, it will work just the same with your arrays. For example here is one where I concatenate 3 random arrays with shapes 3x4x5, 4x5x6 and 35x19x97. EDIT. I can't get it to display nice in the comments here so I will add it to the answer above.
    – James Fulton
    Nov 13 '18 at 19:33













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265071%2fhow-to-concatenate-arrays-with-different-size-in-list-or-array%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









1














It depends on what you want to do with this structure. For instance, if there is some value unique to m0xn0xk0, m1xn1xk1, m2xn2xk2... that you want to index by then you would use a dictionary like this:



A_concate = {}
A_concate[some_key] = m0xn0xk0
A_concate[some_other_key] = m1xn1xk1


and then you could access them using the key values. On the other hand, if you just want all these lists to live on the same object and you're only going to iterate over them, using a list is fine:



def listify(*args):
return list(args)

A_concate = listify(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


Finally, if you just want all the values of these objects in the same place, then you could just concatenate them into one list:



def flatten(*lists):
a =
for l in lists:
a.extend(l)
return a

A_concate = flatten(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


It just depends on what you want to do with it.






share|improve this answer





















  • Thanks. It worked.
    – Moon Lee
    Nov 12 '18 at 15:38










  • Hi. If I want to concatenate them in loop. How can I modify your code? I have write a script at here but it does not show correct dimension repl.it/repls/LightGrotesqueCodeview
    – Moon Lee
    Nov 12 '18 at 15:44










  • Could you look at my issue? It is missing one dims
    – Moon Lee
    Nov 12 '18 at 15:59










  • Of course it is. extend unpacks your list and adds each element to the list you're extending
    – Woody1193
    Nov 12 '18 at 16:08






  • 1




    @MoonLee Sorry about that, I've been doing a lot of C# lately so I got turned around. Use append instead :)
    – Woody1193
    Nov 12 '18 at 16:32
















1














It depends on what you want to do with this structure. For instance, if there is some value unique to m0xn0xk0, m1xn1xk1, m2xn2xk2... that you want to index by then you would use a dictionary like this:



A_concate = {}
A_concate[some_key] = m0xn0xk0
A_concate[some_other_key] = m1xn1xk1


and then you could access them using the key values. On the other hand, if you just want all these lists to live on the same object and you're only going to iterate over them, using a list is fine:



def listify(*args):
return list(args)

A_concate = listify(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


Finally, if you just want all the values of these objects in the same place, then you could just concatenate them into one list:



def flatten(*lists):
a =
for l in lists:
a.extend(l)
return a

A_concate = flatten(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


It just depends on what you want to do with it.






share|improve this answer





















  • Thanks. It worked.
    – Moon Lee
    Nov 12 '18 at 15:38










  • Hi. If I want to concatenate them in loop. How can I modify your code? I have write a script at here but it does not show correct dimension repl.it/repls/LightGrotesqueCodeview
    – Moon Lee
    Nov 12 '18 at 15:44










  • Could you look at my issue? It is missing one dims
    – Moon Lee
    Nov 12 '18 at 15:59










  • Of course it is. extend unpacks your list and adds each element to the list you're extending
    – Woody1193
    Nov 12 '18 at 16:08






  • 1




    @MoonLee Sorry about that, I've been doing a lot of C# lately so I got turned around. Use append instead :)
    – Woody1193
    Nov 12 '18 at 16:32














1












1








1






It depends on what you want to do with this structure. For instance, if there is some value unique to m0xn0xk0, m1xn1xk1, m2xn2xk2... that you want to index by then you would use a dictionary like this:



A_concate = {}
A_concate[some_key] = m0xn0xk0
A_concate[some_other_key] = m1xn1xk1


and then you could access them using the key values. On the other hand, if you just want all these lists to live on the same object and you're only going to iterate over them, using a list is fine:



def listify(*args):
return list(args)

A_concate = listify(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


Finally, if you just want all the values of these objects in the same place, then you could just concatenate them into one list:



def flatten(*lists):
a =
for l in lists:
a.extend(l)
return a

A_concate = flatten(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


It just depends on what you want to do with it.






share|improve this answer












It depends on what you want to do with this structure. For instance, if there is some value unique to m0xn0xk0, m1xn1xk1, m2xn2xk2... that you want to index by then you would use a dictionary like this:



A_concate = {}
A_concate[some_key] = m0xn0xk0
A_concate[some_other_key] = m1xn1xk1


and then you could access them using the key values. On the other hand, if you just want all these lists to live on the same object and you're only going to iterate over them, using a list is fine:



def listify(*args):
return list(args)

A_concate = listify(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


Finally, if you just want all the values of these objects in the same place, then you could just concatenate them into one list:



def flatten(*lists):
a =
for l in lists:
a.extend(l)
return a

A_concate = flatten(m0xn0xk0, m1xn1xk1, m2xn2xk2, ...)


It just depends on what you want to do with it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 15:22









Woody1193

2,246930




2,246930












  • Thanks. It worked.
    – Moon Lee
    Nov 12 '18 at 15:38










  • Hi. If I want to concatenate them in loop. How can I modify your code? I have write a script at here but it does not show correct dimension repl.it/repls/LightGrotesqueCodeview
    – Moon Lee
    Nov 12 '18 at 15:44










  • Could you look at my issue? It is missing one dims
    – Moon Lee
    Nov 12 '18 at 15:59










  • Of course it is. extend unpacks your list and adds each element to the list you're extending
    – Woody1193
    Nov 12 '18 at 16:08






  • 1




    @MoonLee Sorry about that, I've been doing a lot of C# lately so I got turned around. Use append instead :)
    – Woody1193
    Nov 12 '18 at 16:32


















  • Thanks. It worked.
    – Moon Lee
    Nov 12 '18 at 15:38










  • Hi. If I want to concatenate them in loop. How can I modify your code? I have write a script at here but it does not show correct dimension repl.it/repls/LightGrotesqueCodeview
    – Moon Lee
    Nov 12 '18 at 15:44










  • Could you look at my issue? It is missing one dims
    – Moon Lee
    Nov 12 '18 at 15:59










  • Of course it is. extend unpacks your list and adds each element to the list you're extending
    – Woody1193
    Nov 12 '18 at 16:08






  • 1




    @MoonLee Sorry about that, I've been doing a lot of C# lately so I got turned around. Use append instead :)
    – Woody1193
    Nov 12 '18 at 16:32
















Thanks. It worked.
– Moon Lee
Nov 12 '18 at 15:38




Thanks. It worked.
– Moon Lee
Nov 12 '18 at 15:38












Hi. If I want to concatenate them in loop. How can I modify your code? I have write a script at here but it does not show correct dimension repl.it/repls/LightGrotesqueCodeview
– Moon Lee
Nov 12 '18 at 15:44




Hi. If I want to concatenate them in loop. How can I modify your code? I have write a script at here but it does not show correct dimension repl.it/repls/LightGrotesqueCodeview
– Moon Lee
Nov 12 '18 at 15:44












Could you look at my issue? It is missing one dims
– Moon Lee
Nov 12 '18 at 15:59




Could you look at my issue? It is missing one dims
– Moon Lee
Nov 12 '18 at 15:59












Of course it is. extend unpacks your list and adds each element to the list you're extending
– Woody1193
Nov 12 '18 at 16:08




Of course it is. extend unpacks your list and adds each element to the list you're extending
– Woody1193
Nov 12 '18 at 16:08




1




1




@MoonLee Sorry about that, I've been doing a lot of C# lately so I got turned around. Use append instead :)
– Woody1193
Nov 12 '18 at 16:32




@MoonLee Sorry about that, I've been doing a lot of C# lately so I got turned around. Use append instead :)
– Woody1193
Nov 12 '18 at 16:32













1














If that is all you want to do then why not just put them in a list?



You could use



A_concate = [a_0, a_1, a_2,..., a_n]


If you really want to use an array you could use



import numpy as np
A_concate = np.array([a_0, a_1, a_2,..., a_n])


The above will give you an array of objects. Where each entry is an array. You won't have the full functionality to slice across the internal arrays but it depends on what you want to do



Here is a minimal example of the above



import numpy as np
a_0 = np.arange(9)
a_1 = np.arange(10)
a_2 = np.arange(16)
A_concate = np.array([a_0, a_1, a_2])


EDIT



Add another example to show it works more generally than for 1D arrays



import numpy as np
a_0 = np.random.normal(0,1,size=(3,4,5,))
a_1 = np.random.normal(0,1,size=(4,5,6,))
a_2 = np.random.normal(0,1,size=(35,19,97,))
a_3 = np.random.normal(0,1,size=(2,6,14,22,89))
A_concate = np.array([a_0, a_1, a_2, a_3])





share|improve this answer























  • Sorry. there arrays have different size. Could you show the example with size of 3x4x5 for a_0, 4x5x6 for a_1...
    – Moon Lee
    Nov 12 '18 at 15:33










  • Yeh, it will work just the same with your arrays. For example here is one where I concatenate 3 random arrays with shapes 3x4x5, 4x5x6 and 35x19x97. EDIT. I can't get it to display nice in the comments here so I will add it to the answer above.
    – James Fulton
    Nov 13 '18 at 19:33


















1














If that is all you want to do then why not just put them in a list?



You could use



A_concate = [a_0, a_1, a_2,..., a_n]


If you really want to use an array you could use



import numpy as np
A_concate = np.array([a_0, a_1, a_2,..., a_n])


The above will give you an array of objects. Where each entry is an array. You won't have the full functionality to slice across the internal arrays but it depends on what you want to do



Here is a minimal example of the above



import numpy as np
a_0 = np.arange(9)
a_1 = np.arange(10)
a_2 = np.arange(16)
A_concate = np.array([a_0, a_1, a_2])


EDIT



Add another example to show it works more generally than for 1D arrays



import numpy as np
a_0 = np.random.normal(0,1,size=(3,4,5,))
a_1 = np.random.normal(0,1,size=(4,5,6,))
a_2 = np.random.normal(0,1,size=(35,19,97,))
a_3 = np.random.normal(0,1,size=(2,6,14,22,89))
A_concate = np.array([a_0, a_1, a_2, a_3])





share|improve this answer























  • Sorry. there arrays have different size. Could you show the example with size of 3x4x5 for a_0, 4x5x6 for a_1...
    – Moon Lee
    Nov 12 '18 at 15:33










  • Yeh, it will work just the same with your arrays. For example here is one where I concatenate 3 random arrays with shapes 3x4x5, 4x5x6 and 35x19x97. EDIT. I can't get it to display nice in the comments here so I will add it to the answer above.
    – James Fulton
    Nov 13 '18 at 19:33
















1












1








1






If that is all you want to do then why not just put them in a list?



You could use



A_concate = [a_0, a_1, a_2,..., a_n]


If you really want to use an array you could use



import numpy as np
A_concate = np.array([a_0, a_1, a_2,..., a_n])


The above will give you an array of objects. Where each entry is an array. You won't have the full functionality to slice across the internal arrays but it depends on what you want to do



Here is a minimal example of the above



import numpy as np
a_0 = np.arange(9)
a_1 = np.arange(10)
a_2 = np.arange(16)
A_concate = np.array([a_0, a_1, a_2])


EDIT



Add another example to show it works more generally than for 1D arrays



import numpy as np
a_0 = np.random.normal(0,1,size=(3,4,5,))
a_1 = np.random.normal(0,1,size=(4,5,6,))
a_2 = np.random.normal(0,1,size=(35,19,97,))
a_3 = np.random.normal(0,1,size=(2,6,14,22,89))
A_concate = np.array([a_0, a_1, a_2, a_3])





share|improve this answer














If that is all you want to do then why not just put them in a list?



You could use



A_concate = [a_0, a_1, a_2,..., a_n]


If you really want to use an array you could use



import numpy as np
A_concate = np.array([a_0, a_1, a_2,..., a_n])


The above will give you an array of objects. Where each entry is an array. You won't have the full functionality to slice across the internal arrays but it depends on what you want to do



Here is a minimal example of the above



import numpy as np
a_0 = np.arange(9)
a_1 = np.arange(10)
a_2 = np.arange(16)
A_concate = np.array([a_0, a_1, a_2])


EDIT



Add another example to show it works more generally than for 1D arrays



import numpy as np
a_0 = np.random.normal(0,1,size=(3,4,5,))
a_1 = np.random.normal(0,1,size=(4,5,6,))
a_2 = np.random.normal(0,1,size=(35,19,97,))
a_3 = np.random.normal(0,1,size=(2,6,14,22,89))
A_concate = np.array([a_0, a_1, a_2, a_3])






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 19:37

























answered Nov 12 '18 at 15:20









James Fulton

1765




1765












  • Sorry. there arrays have different size. Could you show the example with size of 3x4x5 for a_0, 4x5x6 for a_1...
    – Moon Lee
    Nov 12 '18 at 15:33










  • Yeh, it will work just the same with your arrays. For example here is one where I concatenate 3 random arrays with shapes 3x4x5, 4x5x6 and 35x19x97. EDIT. I can't get it to display nice in the comments here so I will add it to the answer above.
    – James Fulton
    Nov 13 '18 at 19:33




















  • Sorry. there arrays have different size. Could you show the example with size of 3x4x5 for a_0, 4x5x6 for a_1...
    – Moon Lee
    Nov 12 '18 at 15:33










  • Yeh, it will work just the same with your arrays. For example here is one where I concatenate 3 random arrays with shapes 3x4x5, 4x5x6 and 35x19x97. EDIT. I can't get it to display nice in the comments here so I will add it to the answer above.
    – James Fulton
    Nov 13 '18 at 19:33


















Sorry. there arrays have different size. Could you show the example with size of 3x4x5 for a_0, 4x5x6 for a_1...
– Moon Lee
Nov 12 '18 at 15:33




Sorry. there arrays have different size. Could you show the example with size of 3x4x5 for a_0, 4x5x6 for a_1...
– Moon Lee
Nov 12 '18 at 15:33












Yeh, it will work just the same with your arrays. For example here is one where I concatenate 3 random arrays with shapes 3x4x5, 4x5x6 and 35x19x97. EDIT. I can't get it to display nice in the comments here so I will add it to the answer above.
– James Fulton
Nov 13 '18 at 19:33






Yeh, it will work just the same with your arrays. For example here is one where I concatenate 3 random arrays with shapes 3x4x5, 4x5x6 and 35x19x97. EDIT. I can't get it to display nice in the comments here so I will add it to the answer above.
– James Fulton
Nov 13 '18 at 19:33




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265071%2fhow-to-concatenate-arrays-with-different-size-in-list-or-array%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma