Hey. I'm importing an array in a text file via array=numpy.loadtxt("testdata.txt") .
Is there an easy way to count the number of rows and columns? Maybe something like x=numpy.countrow(array) ?
I've had a look around and couldn't find anything suitable.
Thanks, Alex

Recommended Answers

All 2 Replies

Use numpy shape ...

import numpy as np

# create a 3 by 5 array of integer zeroes
z = np.zeros([3, 5], int)
print( z )
"""
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
"""

# check the shape
print( "shape --> %d rows x %d columns" % z.shape )
"""
[[0 0 0 0 0]
 [0 0 0 0 0]
 [0 0 0 0 0]]
shape --> 3 rows x 5 columns
"""

I found out from a friend.

x = array.shape[0] # sets x as the number of rows
y = array.shape[1] # sets y as the number of columns

Thanks for your input anyway! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.