0

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
  • It works fine for me. Can you provide more information about what versions you're using? – Unmitigated Apr 08 '23 at 04:46
  • @Unmitigated - New findings posted, which change the game. – sancho.s ReinstateMonicaCellio Apr 08 '23 at 12:00
  • 1
    So creating the dict works fine, but displaying it gives problems? Does the `print(d)` work? `print` uses the `str(d)` display, while the interactive display uses `repr(d)`. It also uses your `pprint`, as shown by the traceback. I'm not familiar with `R2\App\Python\lib\pprint.py"` – hpaulj Apr 08 '23 at 17:39
  • Does this pprint have problems with other sympy expressions?, e.g. just the symbol `x`, or some expression like `x/y`? – hpaulj Apr 08 '23 at 19:40
  • 1
    Where are you calling `pprint()`? You need to use `sort_dicts=False` so it doesn't try to sort the dictionaries. https://docs.python.org/3/library/pprint.html#pprint.PrettyPrinter – Barmar Apr 09 '23 at 02:15
  • @hpaulj - Yes, as stated, `print(d)` works fine. `print()` <-> `str()` and Interactive <-> `repr`/`pprint` is likely part of the cause. – sancho.s ReinstateMonicaCellio Apr 09 '23 at 10:53
  • @hpaulj - Does `pprint` have other problems? This is the first time I find one. No problems with `x` or `x/y`. It is probably the combination with some particular `dict`s, see comment by Barmar. – sancho.s ReinstateMonicaCellio Apr 09 '23 at 10:57
  • @Barmar - I am not calling `pprint` explicitly. It is likely behind the Interactive display, see exchange with hpaulj. My `pprint` (<3.7) does not have `sort_dicts`, see https://stackoverflow.com/questions/25683088/disabling-sorting-mechanism-in-pprint-output. I guess the "Why...?" part of the OP is answered. For the "How to...?" I will have to dig a little further. – sancho.s ReinstateMonicaCellio Apr 09 '23 at 11:04
  • @hpaulj - Will shortly remove the comments at the beginning, to reduce the clutter. – sancho.s ReinstateMonicaCellio Apr 09 '23 at 11:05
  • @Barmar - Will shortly remove the comments at the beginning, to reduce the clutter. – sancho.s ReinstateMonicaCellio Apr 09 '23 at 11:05
  • What interactive environment are you using that automatically calls `pprint()`? Check whether there's a way to make it print normally. – Barmar Apr 09 '23 at 20:06

1 Answers1

2

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.

hpaulj
  • 221,503
  • 14
  • 230
  • 353