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!

1
  • 2
    Why do you import numpy? Do you want to create an narray? Commented May 22, 2013 at 3:03

2 Answers 2

3
>>> rows = 486
>>> columns = 5
>>> A = [[None] * columns for x in xrange(rows)]
>>> len(A)
486
>>> len(A[0])
5
Sign up to request clarification or add additional context in comments.

Comments

0
rows = int(raw_input("Number rows: "))
cols = int(raw_input("Number cols: "))

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

Output: #rows = 3, cols = 2

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

For your purpose set rows = 1

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

Comments

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.