TypeError: cannot determine truth value of Relational with dict(zip()), only with direct output

61 Views Asked by At

EDIT: The problem only appears when trying to get "direct output" of the result of dict(zip(.... This code, with output via print, works fine

import sympy as sp
x, y = sp.symbols('x,y')
d = dict(zip([x, y], [1, 1]))
print(d)

but simply entering dict(zip([x, y], [1, 1])) throws the error.


Code as simple as this
import sympy as sp
x, y = sp.symbols('x,y')
dict(zip([x, y], [1, 1]))

Produces error

TypeError: cannot determine truth value of Relational

Why, and how can I solve this? If not using OrderedDict, that would be most convenient.

It seems this OP has no problem in doing what is giving me the error (it has a different problem).

Full code, etc. I know it's an old python version, but I guessed it should work.

>>> x, y = sp.symbols('x,y')
>>> dict(zip([x, y], [1, 2]))
Traceback (most recent call last):
  File "<string>", line 449, in runcode
  File "<interactive input>", line 1, in <module>
  File "<string>", line 692, in pphook
  File "C:\development\Portable Python 3.6.5 x64 R2\App\Python\lib\pprint.py", line 53, in pprint
    printer.pprint(object)
  File "C:\development\Portable Python 3.6.5 x64 R2\App\Python\lib\pprint.py", line 139, in pprint
    self._format(object, self._stream, 0, 0, {}, 0)
  File "C:\development\Portable Python 3.6.5 x64 R2\App\Python\lib\pprint.py", line 161, in _format
    rep = self._repr(object, context, level)
  File "C:\development\Portable Python 3.6.5 x64 R2\App\Python\lib\pprint.py", line 393, in _repr
    self._depth, level)
  File "C:\development\Portable Python 3.6.5 x64 R2\App\Python\lib\pprint.py", line 405, in format
    return _safe_repr(object, context, maxlevels, level)
  File "C:\development\Portable Python 3.6.5 x64 R2\App\Python\lib\pprint.py", line 511, in _safe_repr
    items = sorted(object.items(), key=_safe_tuple)
  File "C:\development\Portable Python 3.6.5 x64 R2\App\Python\lib\site-packages\sympy\core\relational.py", line 384, in __nonzero__
    raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
1

There are 1 best solutions below

0
hpaulj On

In an upto date sympy environment (ipthon with isympy) I can display a dict with symbols either with the repr or str (the print is easier to copy-n-paste):

In [70]: print(d)
{x: 1, y: 1}

But if I try to sort the dict, I get your error:

In [71]: sorted(d)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[71], line 1
----> 1 sorted(d)

File ~\miniconda3\lib\site-packages\sympy\core\relational.py:511, in Relational.__bool__(self)
    510 def __bool__(self):
--> 511     raise TypeError("cannot determine truth value of Relational")

TypeError: cannot determine truth value of Relational

Using the standard pprint also invokes this sorting and error:

In [72]: import pprint
In [73]: pprint.pprint(d)

but turning off the sorting

In [74]: pprint.pprint(d, sort_dicts=False)
{x: 1, y: 1}

The problem is that sorting needs to determine an ascending order. That's impossible for symbols. x>y is a Relational, that doesn't have have clean boolean value:

In [80]: bool(x>y)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[80], line 1
----> 1 bool(x>y)

File ~\miniconda3\lib\site-packages\sympy\core\relational.py:511, in Relational.__bool__(self)
    510 def __bool__(self):
--> 511     raise TypeError("cannot determine truth value of Relational")

TypeError: cannot determine truth value of Relational

The topic of dict sorting came up recently in a SO about [argparse]. Its args Namespace object used to sort its attributes on display, but that sorting was removed in 2020. A core developer speculated that in old Python dicts, the keys were presented in an non-determinant order ("random"), so sort was often desirable. But now dict preserves the order in which keys are first assigned. The user has more control over the display order, and doesn't need the sorting (as often). The sorting in pprint probably has the same roots.