[Python-Dev] Re: Comparing dict.values()

2019年7月25日 01:42:47 -0700

On 7/25/19 2:25 AM, David Mertz wrote:
Exactly! that was my thought that the exception message could hint at likely approaches. The NumPy example seems to have a good pattern:
arr1 == arr2
|ValueError:Thetruth value of an array withmore than one element |isambiguous.
|Usea.any()ora.all().|
It's not the equality operator that errors: `==` means element-wise comparison in Numpy.
The error would come from a conversion of the array to bool:
>>> numpy.array([1, 2, 3]) == numpy.array([1, 3, 4])
array([ True, False, False])
>>> if numpy.array([ True, False, False]):
... print('Same!')
...
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Numpy currently returns False when `==` "doesn't make sense", but apparently has plans to change that:
>>> numpy.array([1, 2, 3]) == numpy.array([1, 2])
__main__:1: DeprecationWarning: elementwise comparison failed; this will raise an error in the future.
False
>>> numpy.array([1, 2, 3]) == numpy.array(['a', 'b'])
__main__:1: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
False
>>> numpy.__version__
'1.16.4'
On Wed, Jul 24, 2019, 8:06 PM Rob Cliffe via Python-Dev <[email protected] <mailto:[email protected]>> wrote:
 On 25/07/2019 00:09:37, David Mertz wrote:
 > I agree with Greg.
 >
 > There are various possible behaviors that might make sense, but
 having
 > `d.values() != d.values()` is about the only one I can see no
 sense in.
 +1
 >
 > This really feels like a good cade for reading a descriptive
 > exception. If someone wants too compare `set(d.values())` that's
 > great. If they want `list(d.values())`, also a sensible question.
 But
 > the programmer should spell it explicitly.
 >
 >
 So, a helpful error message including something like "Cannot compare
 dict.values directly, consider converting to sets / lists / sorted
 lists
 before comparing" ?
 _______________________________________________
 Python-Dev mailing list -- [email protected]
 <mailto:[email protected]>
 To unsubscribe send an email to [email protected]
 <mailto:[email protected]>
 https://mail.python.org/mailman3/lists/python-dev.python.org/
 Message archived at
 
https://mail.python.org/archives/list/[email protected]/message/CSTSLCDEJYKDLADQV5PJRCSSVTMB5RIG/
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/2IMLY36A6K45LHCXO2OVQJZBQKL47U6T/
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/DRG757EQKEDNKPPM2KXMZ5NVVSBSGMCL/

Reply via email to