Python tuple Operation Instance parsing _python

Source: Internet
Author: User

This article describes the Python tuple operation methods, shared for everyone to reference. The specific analysis is as follows:

In general, Python's functional usage is very flexible, and C, PHP usage is not the same, and JS is very similar.

When you follow the action, you can find a very magical phenomenon:

>>> T = (1, 3, ' B ')
>>> q = t + ((3, ' abc '))
>>> Q
(1, 3, ' B ', 3, ' abc ')

What I expected here was (1, 3, ' B ', (3, ' abc '), but it turns out to be (1, 3, ' B ', 3, ' abc '), and at first I guess Python is extracting all of the elements back together in the original order. Then I tried it again:

>>> q = t + ((3, ' abc '), ' 3 ')
>>> Q
(1, 3, ' B ', (3, ' abc '), ' 3 ')

Guess wrong, then why q = t + ((3, ' abc ') to split the tuple, I tried again:

>>> q = t + ((3, ' abc ', (' a '))
>>> Q
(1, 3, ' B ', 3, ' abc ', ' A ')

Python also removes the (' a ') from the tuple in the element, and in order to verify my thoughts, I test it further:

>>> q = t + ((3, ' abc ', (' A ', ' ab '))
>>> Q
(1, 3, ' B ', 3, ' abc ', (' A ', ' AB '))

It seems that the results are clear, in the case of A + operation on a tuple, Python will automatically parse the added tuple, and, in keeping with the primitive group structure, parse it into the simplest tuple to add, that is, to bracket the multiple tuples with only a single element.
So, if I don't want Python to get rid of it in the + operation, then that's fine:

>>> q = t + ((3, ' abc '),)
>>> Q
(1, 3, ' B ', (3, ' abc '))

I hope this article will help you with your study of Python programming.

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.