Why does this dictionary, returned by Sympy, appear to have and also not have the key 'w'?
Question:
I have a dictionary returned by Sympy. It appears to have an entry with a key, w
.
>>> stationary_points
{w: 542.962336871418, b: 0.887349275879595}
>>> type(stationary_points)
<class 'dict'>
And I don’t know why but it also appears not to have an entry with the key w
:
>>> stationary_points.get('w')
None
>>> stationary_points['w']
KeyError Traceback (most recent call last)
KeyError: 'w'
Does this dictionary have key w
or not? Why the apparently contradictory behaviour?
Answers:
It’s possible that the w
you see in the dictionary isn’t a string. You can try:
for k in stationary_points:
print(k, stationary_points[k], type(k))
to see what type of objects you’re dealing with.
looks like the key names in the dictionary must be either string or number. It should not be a variable. Example,
stationary_points = {'w': 542.962336871418, 'b': 0.887349275879595}
print(stationary_points, type(stationary_points), stationary_points.get('w'))
# Output -> {'w': 542.962336871418, 'b': 0.887349275879595} <class 'dict'> 542.962336871418
Short answer
The keys are not of type string
they are of type <class 'sympy.core.symbol.Symbol'>
. And to get key values you can do this:
>>> from sympy import Symbol
>>> stationary_points[Symbol('w')]
542.962336871418
>>> stationary_points[Symbol('b')]
0.887349275879595
Long answer
A python dictionary can have keys of type other than str
for example:
class myClass:
pass
a = 1.1
obj = myClass()
mydict = {
'str': 'str',
1: 1,
a: 1.1,
obj: 'my obj'
}
Now if we print that dictionary we get this result:
>>> mydict
{
'str': 'str',
1: 1,
1.1: 1.1,
<__main__.myClass object at 0x00000200B0297908>: 'my obj'
}
Here we can clearly see that only first key has quotes around it and not the others. And this tells that only first one is of type string not the others. And if you want to get non string key values then you’ll have to provide that object.
>>> mydict['str']
'str'
>>> mydict[1]
1
>>> mydict[1.1]
1.1
>>> mydict[a] # We can also provide dynamic key with variables
1.1
>>> mydict[obj]
'my obj'
Now in your case the keys are not of type string but some other type because they don’t have quotes around them:
>>> stationary_points
{w: 542.962336871418, b: 0.887349275879595}
You can see the type of key using this method
>>> type(list(stationary_points.keys())[0])
<class 'sympy.core.symbol.Symbol'>
So the keys are actually of type sympy Symbol
class. So now you can access the value using this method:
>>> from sympy import Symbol
>>> stationary_points[Symbol('w')]
542.962336871418
>>> stationary_points[Symbol('b')]
0.887349275879595
I have a dictionary returned by Sympy. It appears to have an entry with a key, w
.
>>> stationary_points
{w: 542.962336871418, b: 0.887349275879595}
>>> type(stationary_points)
<class 'dict'>
And I don’t know why but it also appears not to have an entry with the key w
:
>>> stationary_points.get('w')
None
>>> stationary_points['w']
KeyError Traceback (most recent call last)
KeyError: 'w'
Does this dictionary have key w
or not? Why the apparently contradictory behaviour?
It’s possible that the w
you see in the dictionary isn’t a string. You can try:
for k in stationary_points:
print(k, stationary_points[k], type(k))
to see what type of objects you’re dealing with.
looks like the key names in the dictionary must be either string or number. It should not be a variable. Example,
stationary_points = {'w': 542.962336871418, 'b': 0.887349275879595}
print(stationary_points, type(stationary_points), stationary_points.get('w'))
# Output -> {'w': 542.962336871418, 'b': 0.887349275879595} <class 'dict'> 542.962336871418
Short answer
The keys are not of type string
they are of type <class 'sympy.core.symbol.Symbol'>
. And to get key values you can do this:
>>> from sympy import Symbol
>>> stationary_points[Symbol('w')]
542.962336871418
>>> stationary_points[Symbol('b')]
0.887349275879595
Long answer
A python dictionary can have keys of type other than str
for example:
class myClass:
pass
a = 1.1
obj = myClass()
mydict = {
'str': 'str',
1: 1,
a: 1.1,
obj: 'my obj'
}
Now if we print that dictionary we get this result:
>>> mydict
{
'str': 'str',
1: 1,
1.1: 1.1,
<__main__.myClass object at 0x00000200B0297908>: 'my obj'
}
Here we can clearly see that only first key has quotes around it and not the others. And this tells that only first one is of type string not the others. And if you want to get non string key values then you’ll have to provide that object.
>>> mydict['str']
'str'
>>> mydict[1]
1
>>> mydict[1.1]
1.1
>>> mydict[a] # We can also provide dynamic key with variables
1.1
>>> mydict[obj]
'my obj'
Now in your case the keys are not of type string but some other type because they don’t have quotes around them:
>>> stationary_points
{w: 542.962336871418, b: 0.887349275879595}
You can see the type of key using this method
>>> type(list(stationary_points.keys())[0])
<class 'sympy.core.symbol.Symbol'>
So the keys are actually of type sympy Symbol
class. So now you can access the value using this method:
>>> from sympy import Symbol
>>> stationary_points[Symbol('w')]
542.962336871418
>>> stationary_points[Symbol('b')]
0.887349275879595