[Tutor] dictionaries are same but returning false

shubham goyal skgoyal721 at gmail.com
Wed Jul 5 13:39:38 EDT 2017


On Jul 5, 2017 11:09 PM, "shubham goyal" <skgoyal721 at gmail.com> wrote:

> Yha that's very smart. Only experience can drive you this way. Otherwise I
> was checking value by value.
>
> On Jul 5, 2017 10:21 PM, "Ashfaq" <quazi.ashfaq at gmail.com> wrote:
>
> Hi Peter,
> The way you find the issue is really cool! Very cool! :)
>
> On Wed, Jul 5, 2017 at 6:10 PM, shubham goyal <skgoyal721 at gmail.com>
> wrote:
>
>> Thank you Peter.
>> Silly mistakes 😀
>>
>> On Jul 5, 2017 5:10 PM, "Peter Otten" <__peter__ at web.de> wrote:
>>
>> > 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
>> >
>> >
>> > _______________________________________________
>> > Tutor maillist  -  Tutor at python.org
>> > To unsubscribe or change subscription options:
>> > https://mail.python.org/mailman/listinfo/tutor
>> >
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
>
>


More information about the Tutor mailing list