0

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

2
Contributors
2
Replies
5
Views
8 Years
Discussion Span
Last Post by Chromana
0

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
"""

Edited by vegaseat: n/a

1

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! :)

Edited by Chromana: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.