[Tutor] dictionaries are same but returning false
Peter Otten
__peter__ at web.de
Wed Jul 5 07:39:54 EDT 2017
shubham goyal wrote:
> null=None
> x={'_udp_options': None, '_icmp_options': None, 'attribute_map':
> {'icmp_options': 'icmpOptions', 'protocol': 'protocol', 'source':
> {'source',
> 'tcp_options': 'tcpOptions', 'is_stateless': 'isStateless', 'udp_options':
> 'udpOptions'}, '_is_stateless': False, 'swagger_types': {'icmp_options':
> 'IcmpOptions', 'protocol': 'str', 'source': 'str', 'tcp_options':
> 'TcpOptions', 'is_stateless': 'bool', 'udp_options': 'UdpOptions'},
> '_protocol': '6', '_source': '0.0.4.0/24', '_tcp_options': {
> "destination_port_range": {
> "max": "22",
> "min": "22"
> },
> "source_port_range": null
> }}
>
> y={'_udp_options': None, '_icmp_options': None, 'attribute_map':
> {'icmp_options': 'icmpOptions', 'protocol': 'protocol', 'source':
> {'source',
> 'tcp_options': 'tcpOptions', 'is_stateless': 'isStateless', 'udp_options':
> 'udpOptions'}, '_is_stateless': False, 'swagger_types': {'icmp_options':
> 'IcmpOptions', 'protocol': 'str', 'source': 'str', 'tcp_options':
> 'TcpOptions', 'is_stateless': 'bool', 'udp_options': 'UdpOptions'},
> '_protocol': '6', '_source': '0.0.4.0/24', '_tcp_options': {
> "destination_port_range": {
> "max": 22,
> "min": 22
> },
> "source_port_range": null
> }}
> if x==y:
> print "true"
> else:
> print "false"
>
>
> These dictionaries are same exactly. but its returning false. i don't
> understand
> what to do?
Let's narrow down the problem, with the help of the interactive interpreter:
>>> changed = [k for k in x if x[k] != y[k]]
>>> changed
['_tcp_options']
>>> k, = changed
A closer look:
>>> x[k]
{'source_port_range': None, 'destination_port_range': {'max': '22', 'min':
'22'}}
>>> y[k]
{'source_port_range': None, 'destination_port_range': {'max': 22, 'min':
22}}
So x uses strings for min/max while y uses integers, and those do not
compare equal in Python:
>>> 22 == "22"
False
Once you fix this
>>> x[k]["destination_port_range"]["max"] = 22
>>> x[k]["destination_port_range"]["min"] = 22
you get the expected result:
>>> x == y
True
More information about the Tutor
mailing list