How to add columns to python arrays

C

CC

Hi there,

I wanna compile a 6000x1000 array with python. The array starts from
'empty', each time I get a 6000 length list, I wanna add it to the
exist array as a column vector. Is there any function to do so?

Or, I can add the list as a rows, if this is easier, and transpose the
whole array after all the rows are setup.

Thanks so much.
 
T

Terry Reedy

CC said:
I wanna compile a 6000x1000 array with python. The array starts from
'empty', each time I get a 6000 length list, I wanna add it to the
exist array as a column vector. Is there any function to do so?

Or, I can add the list as a rows, if this is easier, and transpose the
whole array after all the rows are setup.

Python does not come with a 2D array type either as a builtin type or as a
standard library module. You can only simulate one with a sequence of
sequences (list of lists, for instance). You could initialize as follows:

aray = []
for l6000 in source(): aray.append(l6000)

While this will print as a list of rows, you are free to think of it as a
list of columns.

That said, I suspect you should use the 3rd party NumPy package which
defines multiple-dimensional arrays of several base types.

Terry Jan Reedy
 
M

Michael Spencer

Terry said:
CC said:
I wanna compile a 6000x1000 array with python. The array starts from
'empty', each time I get a 6000 length list, I wanna add it to the
exist array as a column vector. Is there any function to do so?

Or, I can add the list as a rows, if this is easier, and transpose the
whole array after all the rows are setup.

Python does not come with a 2D array type either as a builtin type or as a
standard library module. You can only simulate one with a sequence of
sequences (list of lists, for instance). You could initialize as follows:

aray = []
for l6000 in source(): aray.append(l6000)

While this will print as a list of rows, you are free to think of it as a
list of columns.

That said, I suspect you should use the 3rd party NumPy package which
defines multiple-dimensional arrays of several base types.

Terry Jan Reedy
If you're just looking for a multi-dimensional array type, and don't need
maximum speed or the vast range of array-processing that numpy offers, then
*pyarray* provides a pure-python single module solution.

the latest pyarray is available with tests and documentation at:
http://svn.brownspencer.com/pyarray/trunk/

Introduction
============
pyarray is a pure-Python implementation of a multi-dimensional array type.

pyarray.ListView and pyarray.ArrayView offer a substantial subset of
numpy.ArrayType functionality, by wrapping standard python 'list' and
'array.array' respectively.

Key features include:
* Views: all subscripting operations apart from individual cell access
access return views to existing 'live' data
* Extended Indexing: slicing, arbitrary 'takes', index arrays etc...
* Unlimited re-shaping: while still addressing one data-source
* Elementwise binary operations: all basic arithmetic and comparison
operations
* Data broadcasting: allows assignment and binary operations between
views of different shapes
* Friendly __repr__: work safely with big arrays at the interactive prompt

Regards
Michael
 
T

Terry Reedy

Michael Spencer said:
If you're just looking for a multi-dimensional array type, and don't need
maximum speed or the vast range of array-processing that numpy offers,
then
*pyarray* provides a pure-python single module solution.

the latest pyarray is available with tests and documentation at:
http://svn.brownspencer.com/pyarray/trunk/

Nice. I downloaded and expect to give it a try sometime. I suggest
putting the url in the module file itself to make it easier to search for
updated versions ;-)

tjr
 
M

Michael Spencer

Terry said:
Nice. I downloaded and expect to give it a try sometime. I suggest
putting the url in the module file itself to make it easier to search for
updated versions ;-)

tjr
Thanks for the suggestion, Terry. I've added a static link in each source file
to the repository at: http://svn.brownspencer.com/pyarray/
and also added an email address. If you do try it, I'd be interested to hear
your feedback.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,527
Members
48,249
Latest member
reactnativeexpert

Latest Threads

Top