Parsing of python tuples and python instances

Source: Internet
Author: User

Parsing of python tuples and python instances

The example in this article describes how to operate python tuples for your reference. The specific analysis is as follows:

In general, the usage of python functions is quite flexible, not the same as that of c and php, but quite similar to that of js.

When you follow the operation, you can find a magical phenomenon:

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

What I expected here is (1, 3, 'B', (3, 'abc'), but the result is (1, 3, 'B', 3, 'abc') at the beginning, I guess python extracts all the elements and then combines them in the original order. Then I tried again:

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

I guess it's wrong. Why q = t + (3, 'abc') split the tuples? I tried again:

>>> q = t + ((3, 'abc', ('a')))>>> q(1, 3, 'b', 3, 'abc', 'a')

Python removes the ('A') in the tuples in the element. to verify my ideas, I further test:

>>> q = t + ((3, 'abc', ('a', 'ab')))>>> q(1, 3, 'b', 3, 'abc', ('a', 'ab'))

It seems that the result is quite clear. When you perform the + operation on the tuples, python will automatically parse the added tuples, while maintaining the original element group structure, parse it into the simplest tuples for addition, that is, remove the multiple tuples with only a single element from the brackets.
So, if I don't want python to remove () during the + operation, this will do:

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

I hope this article will help you learn Python programming.


How to read one of the Python tuples in pairs?

First, this is a list composed of tuples, which can be directly sorted.
>>> S = [('the', 5), ('of', 4), ('A', 3), ('others', 2 ), ('is', 2), ('when', 2), ('beating ', 2)]
>>> S. sort (key = lambda x :( x [1], x [0])
>>> S
[('Beating ', 2), ('is', 2), ('people', 2), ('when', 2), ('A ', 3), ('of', 4), ('the', 5)]

Metagroups in Python

New_zoo = ('monkey', 'dolphe', zoo) = ('monkey', 'dolphe', ('Wolf ', 'elephant', 'penguin '))
Instead of equal to ('monkey', 'dolp', 'monkey', 'dolp', zoo)
So len (new_zoo) is equal to 3. However, its third element is also a tuples.

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.