Differences between the element group and the array when python uses the in operator, and the python Operator

Source: Internet
Author: User

Differences between the element group and the array when python uses the in operator, and the python Operator

In python, you can use the in symbol to determine whether the specified element exists in the list. However, I found that there are differences between tuples and arrays. The following is the detailed experiment result.

>>> 'Jb51. net 'in ['haotu.. net ', 'jb51.. net '] True> 'jb51. net 'in ('haotu.. net ', 'jb51.. net ') True> 'jb51. net 'in ['jb51. net/codes ', 'haotu.. net '] False>' jb51. net in ('jb51. net/codes ', 'haotu.. net ') False>' jb51. net 'in ['jb51. net/codes '] False >>> 'jb51. net in ('jb51. net/codes ') True

The first five tests all conform to our expectation. There is only the last one. After the array is replaced with a tuples, python treats the tuples as strings when there is only one element in the tuples, I don't know why there is such a processing method, but I should pay attention to it during development. When I only use the in statement to determine whether there are elements in the array, it is best to use the [] array instead of the tuples.

The above is all the content of this article. I hope you will like it.

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.