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.