ERROR : while finding size

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

ERROR : while finding size

ESKalaiyarasan
hi,
   I am Kalaiyarasan.I am doing my graduate degree in anna university chennai.I am using python pylab for my project.

in matlab we can get input from user using x=input() command similarly in python we use x=raw_input() command


but i want to how to know size of data, in matlab we use size(x) command return size (example 3 X 2). what is command for python?


please help me.



-Kalaiyarasan

_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: ERROR : while finding size

Joshua Holbrook
Ooh, I got this.

If you're looking at something like a list or a tuple:

    > [in]:  a = [1, 2, 3]
    > [in]:  len(a)
    > [out]: 3

However, if you have a numpy array, you'll want to do this instead:

    > [in]: a = array([1, 2, 3])
    > [in]: a.shape
    > [out]: (3, )

That will give you a tuple with all your dimensions.

If you are already familiar with matlab, I would recommend this guy
for your basic numpy/scipy questions:

    http://www.scipy.org/NumPy_for_Matlab_Users

Cheers,

--Josh


On Wed, Mar 2, 2011 at 11:46 PM, ESKalaiyarasan
<[hidden email]> wrote:

> hi,
>    I am Kalaiyarasan.I am doing my graduate degree in anna university
> chennai.I am using python pylab for my project.
>
> in matlab we can get input from user using x=input() command similarly in
> python we use x=raw_input() command
>
>
> but i want to how to know size of data, in matlab we use size(x) command
> return size (example 3 X 2). what is command for python?
>
>
> please help me.
>
>
>
> -Kalaiyarasan
>
> _______________________________________________
> SciPy-User mailing list
> [hidden email]
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: ERROR : while finding size

Hector troy-2
In reply to this post by ESKalaiyarasan


On Thu, Mar 3, 2011 at 2:16 PM, ESKalaiyarasan <[hidden email]> wrote:
hi,
   I am Kalaiyarasan.I am doing my graduate degree in anna university chennai.I am using python pylab for my project.

in matlab we can get input from user using x=input() command similarly in python we use x=raw_input() command

Hello Kalaiyarasan,
This is an information about raw_input function( I strongly recommend you to switch to IPython if you are not using it yet). The point I would like to highlight is that, raw input returns the *string* and hence may not be very useful if you are looking for an matrix input.

In [17]: raw_input?
Type:        builtin_function_or_method
Base Class:    <type 'builtin_function_or_method'>
String Form:    <built-in function raw_input>
Namespace:    Python builtin
Docstring:
    raw_input([prompt]) -> string
   
    Read a string from standard input.  The trailing newline is stripped.
    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
    On Unix, GNU readline is used if enabled.  The prompt string, if given,
    is printed without a trailing newline before reading.


 


but i want to how to know size of data, in matlab we use size(x) command return size (example 3 X 2). what is command for python?


And with the sting you can always use len(a) function to know the length of string.

In [18]: a = raw_input()
343

In [19]: type(a)
Out[19]: <type 'str'>

In [20]: len(a)
Out[20]: 3

The additional tool I know in this regard is

In [21]: a = int(raw_input())
232

In [22]: type(a)
Out[22]: <type 'int'>

Hope this will help you a bit.
But I am not an expert here so there is a good chance that someone else can give you better answer.
 

please help me.



-Kalaiyarasan


For the  rest of group, kindly save me from misguiding  him if I am not right.
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user




--
-Regards
Hector

Whenever you think you can or you can't, in either way you are right.


_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: ERROR : while finding size

Chris Barker - NOAA Federal
On 3/3/11 1:17 AM, Hector wrote:
>  The point I would
> like to highlight is that, raw input returns the *string* and hence may
> not be very useful if you are looking for an matrix input.

indeed. you may be better off with input(), which does evaluate the
expression and return the appropriate python object:

In [30]: x = raw_input()
[1,2,3,4]

In [31]: x
Out[31]: '[1,2,3,4]'

#so x is a string -- probably not what you want here.

In [32]: x = input()
[1,2,3,4]

In [33]: x
Out[33]: [1, 2, 3, 4]

# now x is a list-- more likely what you want:

In [34]: len(x)
Out[34]: 4


In [35]: x = input()
np.array([1,2,3,4])

In [36]: x
Out[36]: array([1, 2, 3, 4])


In [37]: x.shape
Out[37]: (4,)

now x is a numpy array, most likely what you want, but a bit awkward for
users, but there is nopython lieterl notation for arrays -- only lists
and tuples.

# perhaps you can write it this way:
In [38]: x = np.array(input())
[1,2,3,4]

In [39]: x
Out[39]: array([1, 2, 3, 4])

which gives you an array. You can get 2-d (and higher) arrays this way:

In [40]: x = np.array(input())
[[1,2,3], [4,5,6]]

In [41]: x
Out[41]:
array([[1, 2, 3],
        [4, 5, 6]])

In [42]: x.shape
Out[42]: (2, 3)



HTH,
   -Chris




--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
SciPy-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/scipy-user