1, definition and initialization of tuples
t = tuple ()
t = ()
t = (1,2,3)
t = tuple (range (3)) #通过迭代的方式产生一个元组
2, the operation of a tuple
tuples are immutable, so tuples are not added, deleted, modified operations
tuple queries, which can be queried by subscript (index)
>>> t = (1,2,3,4,5)
>>> T[0]
1
>>> T[3]
4
The index method of the tuple is consistent with the list, and T.index (value) to view the indexed position of the element through the value of the element
>>> t = (1,2,3,4,5)
>>> T.index (2)
1
>>> T.index (5)
4
Another method for tuples is count (), the Count method is consistent with the list, and T.count (value) prints out the number of elements as value
>>> t = (1,1,1,2,3,3,4,5,5,5,5,3)
>>> T.count (1)
3
>>> T.count (3)
3
3, the operation of a tuple
The tuples can be combined and copied by using + or * for tuples, and the operation becomes a new tuple
a new tuple was created T3
>>> T1 = (1,2,3)
>>> t2 = (4,5,6)
>>> t3 = t1 + T2
>>> T3
(1, 2, 3, 4, 5, 6)
>>> T1
(1, 2, 3)
>>> t1*2
(1, 2, 3, 1, 2, 3)
4, the elements in the tuple are not allowed to be deleted, but you can delete tuples by using the DEL statement
del function can be deleted for tuple
>>> T1
(1, 2, 3)
>>> del T1
>>> T1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Nameerror:name ' T1 ' is not defined
5, the interception of the tuple
because tuples are also sequences, they can be intercepted and accessed through the index
>>> T
(1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 5, 3)
>>> T[1:6]
(1, 1, 2, 3, 3)
>>> t[0:100]
(1, 1, 1, 2, 3, 3, 4, 5, 5, 5, 5, 3)
The 6,python tuple contains the following built-in functions
1, CMP (Tuple1, Tuple2): compare two tuple elements.
2, Len (tuple): Calculate the number of tuple elements.
3, Max (tuple): Returns the maximum value of an element in a tuple.
4, min (tuple): Returns the element minimum value in a tuple.
5, tuple (seq): Converts a list to a tuple.
7. Named tuples
We know that there are structures in the C + + language, such as the data type:
struct{
string name;
int age;
char sex;
} Student
1
2
3
4
5
1
2
3
4
5
You can use the. operator when assigning or taking a value to a struct object.
so the question is, is there a data type in Python? The answer is yes, it is a named tuple (namedtyple).
First take a look at the inconvenience of the common tuple in Python:
Bob= ("Bob", "male")
#如果想知道Bobde name, you need to read using the index location,
name=bob[0] for
people in [Bob]:
print ('%s ' %d years old%s '% peole '
#显示结果
Bob is-years old male
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
Namedtuple Foundation
through the example above, access to tuple data is done through index subscripts. In this need to memorize each subscript corresponding to the specific meaning, if the tuple has hundreds of data, then want to remember each subscript corresponding meaning that is very difficult, so there is a named tuple namedtuple.
the Namedtuple object is defined as the following format:
Collections.namedtuple (TypeName, Field_names, Verbose=false, Rename=false)
returns a named tuple subclass TypeName, where the meaning of the parameter is as follows:
TypeName,: The name of this tuple;
Field_names: The name of the element in the tuple (similar to the age in the C structure), this field has a variety of expressions, see examples;
Rename: If the element name contains Python keywords, it must be set to Rename=true, see below;
VERBOSE: The default is good;
to give a small example, deepen your understanding:
Import Collections
#其中field_names There are several ways to express it, as follows
student=collections.namedtuple (' student ', ' name Age sex ')
Student=cpllections.namedtuple (' Student ', [' name ', ' age ', ' sex '])
student=cpllections.namedtuple (' Student ', ' Name,age,sex ')
spark=student (name= ' Sunyang ', age=20,sex= ' male ')
print (spark)
print ("Spark ' s name is %s '% Spark.name '
print ('%s is%d years old%s '% spark)
Displays the results as follows:
student (name= ' Sunyang ', age=20, sex= ' male ')
Spark ' s name is Sunyang
Sunyang was years old male
1
2
3
4
5
6
7
8
9
Ten
One
of
of
-
-
1
2
3
4
5
6
7
8
9
Ten
One
of
of
-
-
In The example above, where student is the tuple name, ' name Age sex ' is the tuple's total element name, separated by spaces, we can access elements in a tuple object using the comma operator (.) to read an element of interest in the object, rather than in a tuple, You need to record the element meaning that the subscript represents.
Here's how the rename parameter works:
Import Collections
with_class=collections.namedtuple (' person ', ' Name Age Class Gender ', Rename=true)
print With_class._fields
two_ages=collections.namedtuple (' person ', ' name Age gender age ', rename=true)
print Two_ Ages._fields its output is:
(' name ', ' age ', ' _2 ', ' Gender ')
(' name ', ' age ', ' gender ', ' _3 ')
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
We open the Rename option using the Rename=true method.
You can see that the class in the first collection is renamed ' 2′; the duplicate age in the second set is renamed ' _3′ ' because namedtuple renamed with the underscore plus the number of indexes in the element.
8, a "variable" tuple
>>> t = (' A ', ' B ', [' A ', ' B '])
>>> t[2][0]= ' X '
>>> t[2][1]= ' Y '
>>> T
(' A ', ' B ', [' X ', ' Y '])
Why the tuple here has changed, not to say tuple can not be changed.
This tuple is defined as having 3 elements, namely ' a ', ' B ' and a list, when we change the list's elements ' a ' and ' B ' to ' X ' and ' Y ', on the surface, the tuple element does change, but in fact it is not the tuple element, but the element of the list. Tuple first point to the list did not change to another list, so, tuple so-called "invariant" is that tuple each element, point to never change. Point to ' a ', can not be changed to point ' B ', point to a list, can not be changed to point to other objects, but the list itself is variable.
After you understand the "point invariant", create a tuple that doesn't change the content. It must be ensured that every element of the tuple itself cannot be changed.
7, a "variable" tuple