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
In an upto date sympy environment (
ipthonwithisympy) I can display adictwith symbols either with therepror str (the print is easier to copy-n-paste):But if I try to sort the dict, I get your error:
Using the standard
pprintalso invokes this sorting and error:but turning off the sorting
The problem is that sorting needs to determine an ascending order. That's impossible for symbols.
x>yis aRelational, that doesn't have have clean boolean value:The topic of dict sorting came up recently in a SO about
[argparse]. ItsargsNamespaceobject 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 nowdictpreserves 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 inpprintprobably has the same roots.