0 Comparison of python-9.3 objects in basic science

Source: Internet
Author: User

1. All Python objects can support comparison operations-test equality, relative size, etc.

Python's comparison is to examine all parts of a compound object until the result is reached, and it will automatically traverse the data structure, from left to right recursively compared, how deep it goes.

= =: to the ratio

is: Compare memory references

The above example, although the values of the two lists are equal, but they are different objects, stored in different memory areas, referencing different memory addresses, so = = operation is the same, is operation is different

Take a look at two more examples (note: Below we will be an example of two strings, in Python, no matter how long a character combination without spaces is a short character, if there is a space in the middle of the string, then he is a long string)

>>> str1= ' abc ' >>> str2= ' abc ' >>> STR1==STR2,STR1 is str2 (true, True)
>>> str1= ' Abcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabce Fghijklmnabcefghijklmnabcefghijklmn ' >>> str2= ' Abcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabce FGHIJKLMNABCEFGHIJKLMNABCEFGHIJKLMN ' >>> str1==str2,str1 is str2 (true, True)

Python takes a caching mechanism for short characters, so either the = = operation or the IS operation returns true

The following example is the same as the above character, except that the space is randomly inserted in the middle of the character, and then Python is the long string

>>> str1= ' Abcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabce Fghijklmnabcefghijklmnabcefghijklmn ' >>> str2= ' Abcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabcefghijklmnabce FGHIJKLMNABCEFGHIJKLMNABCEFGHIJKLMN ' >>> str1==str2,str1 is str2 (true, True)

There is no caching mechanism for long strings, so = = Returns true because values are equal, while the is operation returns false and memory addresses are different

There's a number type like that.

For small shapes, Python uses a caching mechanism, others are not

>>> int1=1>>> int2=1>>> Int1==int2,int1 is Int2 (true, true) >>> int1=1000>> > int2=1000>>> int1==int2,int1 is Int2 (True, False)


2. Comparison of dictionaries

In the previous version of Python3, the dictionary could be of comparable size, but after 3.0 it was canceled.

>>> d1={' A ': 1, ' B ':2}>>> d2={' a ': 1, ' B ':2}>>> d1==d2,d1 is D2 (True, False) >>> d1 <d2traceback (most recent):  File "<pyshell#34>", line 1, in <module>    

Need to compare size, you need to turn the dictionary into a list or use sorted's built-in function




Right here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Comparison of python-9.3 objects in basic science

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.