Re: [Tutor] If tuple cannot be sorted, then why sorted() on a tuple is fine?

2017-08-10 Thread Peter Otten
C W wrote:

> This is a follow up. I actually ran into this today:
> 
> import numpy as np
> xArray = np.ones((3, 4))
> 
>> xArray.shape
> (3, 4)
>> np.shape(xArray)
> (3, 4)
> 
> It was confusing to see that both xArray.shape and np.shape() worked. Are
> they equivalent?

>>> print(inspect.getsource(numpy.shape))
def shape(a):
"""
Return the shape of an array.

Parameters
--
a : array_like
Input array.

Returns
---
shape : tuple of ints
The elements of the shape tuple give the lengths of the
corresponding array dimensions.

See Also

alen
ndarray.shape : Equivalent array method.

Examples

>>> np.shape(np.eye(3))
(3, 3)
>>> np.shape([[1, 2]])
(1, 2)
>>> np.shape([0])
(1,)
>>> np.shape(0)
()

>>> a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
>>> np.shape(a)
(2,)
>>> a.shape
(2,)

"""
try:
result = a.shape
except AttributeError:
result = asarray(a).shape
return result

So no; numpy.shape() tries to convert unshapely objects to an array ;)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If tuple cannot be sorted, then why sorted() on a tuple is fine?

2017-08-09 Thread C W
This is a follow up. I actually ran into this today:

import numpy as np
xArray = np.ones((3, 4))

> xArray.shape
(3, 4)
> np.shape(xArray)
(3, 4)

It was confusing to see that both xArray.shape and np.shape() worked. Are
they equivalent?

In the case of sort() vs sorted(), they are different, but close enough to
mistake them as the same thing.

Thanks!

On Wed, Aug 2, 2017 at 9:32 PM, C W  wrote:

> As pointed out by someone else, ?sorted
> sorted(iterable, key=None, reverse=False)
>
> It seems like the only requirement is iterable. I guess tuple is iterable,
> so, it doesn't break the assumption that tuple is immutable.
>
> That's what I see, am I right in that?
>
> Thanks!
>
> On Wed, Aug 2, 2017 at 4:07 PM, Alan Gauld via Tutor 
> wrote:
>
>> On 02/08/17 20:01, C W wrote:
>>
>> > I am a little confused about why Tuple can be sorted.
>> >
>> > Suppose I have the following,
>> >
>> >> aTuple = (9, 3, 7, 5)
>> >> sorted(aTuple)
>> > [3, 5, 7, 9]
>>
>> sorted() returns a new object.
>> The original tuple has not been changed
>>  - print aTuple to confirm this.
>>
>> HTH
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If tuple cannot be sorted, then why sorted() on a tuple is fine?

2017-08-03 Thread C W
As pointed out by someone else, ?sorted
sorted(iterable, key=None, reverse=False)

It seems like the only requirement is iterable. I guess tuple is iterable,
so, it doesn't break the assumption that tuple is immutable.

That's what I see, am I right in that?

Thanks!

On Wed, Aug 2, 2017 at 4:07 PM, Alan Gauld via Tutor 
wrote:

> On 02/08/17 20:01, C W wrote:
>
> > I am a little confused about why Tuple can be sorted.
> >
> > Suppose I have the following,
> >
> >> aTuple = (9, 3, 7, 5)
> >> sorted(aTuple)
> > [3, 5, 7, 9]
>
> sorted() returns a new object.
> The original tuple has not been changed
>  - print aTuple to confirm this.
>
> HTH
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If tuple cannot be sorted, then why sorted() on a tuple is fine?

2017-08-02 Thread Alan Gauld via Tutor
On 02/08/17 20:01, C W wrote:

> I am a little confused about why Tuple can be sorted.
> 
> Suppose I have the following,
> 
>> aTuple = (9, 3, 7, 5)
>> sorted(aTuple)
> [3, 5, 7, 9]

sorted() returns a new object.
The original tuple has not been changed
 - print aTuple to confirm this.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If tuple cannot be sorted, then why sorted() on a tuple is fine?

2017-08-02 Thread Mats Wichmann
it generated a new object, did not change the original. hint: notice the output 
is a list, not a tuple!

On August 2, 2017 1:01:31 PM MDT, C W  wrote:
>Dear list,
>
>I am a little confused about why Tuple can be sorted.
>
>Suppose I have the following,
>
>> aTuple = (9, 3, 7, 5)
>> sorted(aTuple)
>[3, 5, 7, 9]
>
>Why is it ok to sort a the class tuple? If it is invariant by nature,
>then
>wouldn't applying a function on it yield an error?
>
>Thanks!
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] If tuple cannot be sorted, then why sorted() on a tuple is fine?

2017-08-02 Thread C W
Dear list,

I am a little confused about why Tuple can be sorted.

Suppose I have the following,

> aTuple = (9, 3, 7, 5)
> sorted(aTuple)
[3, 5, 7, 9]

Why is it ok to sort a the class tuple? If it is invariant by nature, then
wouldn't applying a function on it yield an error?

Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor