3

An ASCII file has 61 columns, from which the columns are read using readlines(). The user has the option to specify how many columns to use to create an n-dimennsional array based on his/her choice of number of columns.

I want to create a dynamic n-dimensional array such as:

from numpy import *
FILE = open('test.txt','rb')

Choice = float(raw_input('How many columns do you want to use: \t'))

A = [[],[],[],...]  # N-dimensional array (rows = 486, columns = N)

such that A has the dimensions based on the user's choice 'Choice'. 'N' can change between 1 and 61. How could I go about doing this?

-Thanks!

4

2 回答 2

3
>>> rows = 486
>>> columns = 5
>>> A = [[None] * columns for x in xrange(rows)]
>>> len(A)
486
>>> len(A[0])
5
于 2013-05-22T03:02:42.660 回答
0
rows = int(raw_input("Number rows: "))
cols = int(raw_input("Number cols: "))

a = np.zeros([rows, cols])

输出:#rows = 3,cols = 2

print a
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

为您的目的设置rows = 1

a = np.zeros([1, cols])
于 2013-05-22T09:06:35.620 回答