24

I am using Python/NumPy, and I have two arrays like the following:

array1 = [1 2 3]
array2 = [4 5 6]

And I would like to create a new array:

array3 = [[1 2 3], [4 5 6]]

and append items to it. So for example if the new items to append are:

array4 = [7 8 9]
array5 = [10 11 12]

Then now array3 would be an array with two rows and two columns like the one shown below:

array3= [[1 2 3], [4 5 6]
         [7 8 9], [10 11 12]]

I seem to have problems because the elements of my arrays are not separated by commas.

6
  • 7
    Why not just separate the elements of your arrays by commas? Commented Aug 18, 2012 at 17:54
  • If that's the only option, I guess I will have to do that. I am importing a tab delimited text file to analyze in python, and python keeps the elements separated by a tab. I was hoping the conversion from tab to comma separation would be redundant, but I may be wrong (it seems to be an issue here). Commented Aug 18, 2012 at 18:05
  • What does "python keeps the elements separated by a tab" mean? Are the rows still strings? What does print repr(array1) give? Commented Aug 18, 2012 at 18:17
  • Actually, print repr(array1) gives me array([1,2,3]). Now I am confused... Commented Aug 18, 2012 at 18:37
  • possible duplicate of What's the simplest way to extend a numpy array in 2 dimensions? Commented Aug 18, 2012 at 20:48

5 Answers 5

15

It seems strange that you would write arrays without commas (is that a MATLAB syntax?)

Have you tried going through NumPy's documentation on multi-dimensional arrays?

It seems NumPy has a "Python-like" append method to add items to a NumPy n-dimensional array:

>>> p = np.array([[1,2],[3,4]])

>>> p = np.append(p, [[5,6]], 0)

>>> p = np.append(p, [[7],[8],[9]],1)

>>> p
array([[1, 2, 7], [3, 4, 8], [5, 6, 9]])

It has also been answered already...

From the documentation for MATLAB users:

You could use a matrix constructor which takes a string in the form of a matrix MATLAB literal:

mat("1 2 3; 4 5 6")

or

matrix("[1 2 3; 4 5 6]")

Please give it a try and tell me how it goes.

Sign up to request clarification or add additional context in comments.

6 Comments

The data is imported from a tab delimited text file. Appending items separated with commas is trivial. Still unclear how to append tab delimited data. Trying to find out how to convert the tab delimited stuff into comma-separated data. Not aware that the tab-delimited part has been answered already. Thanks.
Tried editing my answer by guessing the input. If i'm wrong, please update your question with the exact form of imput (i.e: 1 2 3\t 4 5 6 .. etc)
Opted for repr(array) solution (see comment above)
@user1609104 - It sounds like you might be using eval somewhere. You don't need to do this, and moreover you shouldn't. Could you post your code?
p = np.array([[1,2],[3,4]]) p = np.append(p, [[7],[8],[9]],1) ValueError: all the input array dimensions except for the concatenation axis mus t match exactly
|
4

You'll have problems creating lists without commas. It shouldn't be too hard to transform your data so that it uses commas as separating character.

Once you have commas in there, it's a relatively simple list creation operations:

array1 = [1,2,3]
array2 = [4,5,6]

array3 = [array1, array2]

array4 = [7,8,9]
array5 = [10,11,12]

array3 = [array3, [array4, array5]]

When testing we get:

print(array3)

[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]

And if we test with indexing it works correctly reading the matrix as made up of 2 rows and 2 columns:

array3[0][1]
[4, 5, 6]

array3[1][1]
[10, 11, 12]

Hope that helps.

Comments

3

If the file is only numerical values separated by tabs, try using the csv library: http://docs.python.org/library/csv.html (you can set the delimiter to '\t')

If you have a textual file in which every line represents a row in a matrix and has integers separated by spaces\tabs, wrapped by a 'arrayname = [...]' syntax, you should do something like:

import re
f = open("your-filename", 'rb')
result_matrix = []
for line in f.readlines():
    match = re.match(r'\s*\w+\s+\=\s+\[(.*?)\]\s*', line)
    if match is None:
        pass # line syntax is wrong - ignore the line
    values_as_strings = match.group(1).split()
    result_matrix.append(map(int, values_as_strings))

1 Comment

Thanks, just added a comment above.
0

Just use commas. and an easy way to add them to your list is by selecting the array and Ctrl+f to replace spaces by commas. (if contains string replace " " by ",").

1 Comment

Why don’t you add some example code to your answer?
-1
a=np.array([[1,2,3],[4,5,6]])
a.tolist()

tolist method mentioned above will return the nested Python list.

1 Comment

Please add some explanation to your answer to help the original poster and also others that come to read your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.