Public account: You and the cabin by: Peter Editor: Peter
Hello, I’m Peter
Tuples tuple
In the Python data type introduced earlier: list list, we found that list can be modified. But there are times when you need to create a series of elements that cannot be modified, and another ordered data type in Python, a tuple, can do the job.
The overall layout of this article:
How to Write an essay
Recently, a friend asked me: Peter, how do you write an article for an official account?
In the future, I will write a special article to answer this question, mainly from four aspects:
- Before writing
- In the writing
- After writing
- Published articles
There is time to squeeze; Sometimes stay up late and spend weekends writing!
Internet, volume!
Tuples create
- Tuples use parentheses in Python
(a)
Enclosed, the list is in square brackets[]
The enclosed - Elements in tuples are separated by commas
- The elements in a tuple can be any Python data type
- A tuple is a sequence, like a list, but the elements in a tuple cannot be changed
Create an empty tuple
a = ()
Copy the code
a
Copy the code
The following result indicates that an empty tuple was created:
(a)Copy the code
type(a)
Copy the code
tuple
Copy the code
A single element
b = (3.)# numeric
b
Copy the code
(3)Copy the code
type(b) Tuple = tuple
Copy the code
tuple
Copy the code
c = ("python".)The # character type
c
Copy the code
('python',)
Copy the code
type(c)
Copy the code
tuple
Copy the code
d = (["python"."java".3],) There is only one element, and it is a list
d
Copy the code
(['python', 'java', 3],)
Copy the code
type(d)
Copy the code
tuple
Copy the code
Note that when there is only one element in a tuple, it must be followed by a comma, otherwise Python will not consider it a tuple:
e = (3) # no comma, system default is numeric
e
Copy the code
3
Copy the code
type(e)
Copy the code
int
Copy the code
f = (["python"."java"]) There is only one element list
f
Copy the code
['python', 'java']
Copy the code
type(f) The system defaults to list
Copy the code
list
Copy the code
As the above examples show, if you create a tuple with only one element (of any Python type), you must enclose parentheses at the end
Multiple elements
Elements can be of different data types through tuples of multiple elements
t1 = (1.2.3) # all numeric types
t1
Copy the code
(1, 2, 3)
Copy the code
type(t1)
Copy the code
tuple
Copy the code
t2 = (1."pyton"."java") # numeric + string
t2
Copy the code
(1, 'pyton', 'java')
Copy the code
type(t2)
Copy the code
tuple
Copy the code
t3 = (1[1.2.3]."python") # numeric + list + string
t3
Copy the code
(1, [1, 2, 3], 'python')
Copy the code
type(t3)
Copy the code
tuple
Copy the code
Consider this amazing example: when we assign the variable T4, there are three values following it;
As you can see from the results, Python treats them as a whole and puts them into a tuple
t4 = 100."python"."hello"
t4
Copy the code
(100, 'python', 'hello')
Copy the code
type(t4) The data type is a tuple
Copy the code
tuple
Copy the code
Elements in a tuple can also be of tuple type
t5 = (1.2, (3.4.5),"python") The (3,4,5) parts are tuples
t5
Copy the code
(1, 2, (3, 4, 5), 'python')
Copy the code
type(t5)
Copy the code
tuple
Copy the code
Created by the tuple function
When creating with a tuple, enclose the elements in parentheses
tuple1 = tuple((1.3.5.7)) # Two layers of parentheses
tuple1
Copy the code
(1, 3, 5, 7)
Copy the code
type(tuple1)
Copy the code
tuple
Copy the code
If only one layer is used, an error is reported; The tuple method takes only one argument:
tuple(1.3.5.7)
Copy the code
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-b16145832f5a> in <module>
----> 1 tuple(1,3,5,7)
TypeError: tuple expected at most 1 arguments, got 4
Copy the code
tuple2 = tuple((1."c".5."python"))
tuple2
Copy the code
tuple3 = tuple(((1.2.3].3."python".7))
tuple3
Copy the code
Zip function creation
Zip is a higher-order function in Python, and its use will be explained later. We can also use it to create tuples
name = ["Xiao Ming"."Little red"."Chou"]
age = [20.28.19]
zip(name,age) Generate a zip object
Copy the code
<zip at 0x107d34320>
Copy the code
list(zip(name,age)) The object is transformed into a list, in which the elements are tuples
Copy the code
[(' Xiao Ming ', 20), (' Xiao Hong ', 28), (' Xiao Zhou ', 19)]Copy the code
tuple(zip(name,age)) # 2, convert to nested tuples in tuples
Copy the code
(' Xiao Ming ', 20), (' Xiao Hong ', 28), (' Xiao Zhou ', 19)Copy the code
dict(zip(name,age)) It can also be converted to a dictionary (which is also a Python data type).
Copy the code
{' Xiao Ming ': 20, 'Xiao Hong ': 28,' Xiao Zhou ': 19}Copy the code
Basic tuple operations
For the length of the
t6 = (0.1.2.3.4.5.6.7.8) # Purely numerical
t6
Copy the code
(0, 1, 2, 3, 4, 5, 6, 7, 8)
Copy the code
t7 = ("python"."java"."c") # character type
Copy the code
type(t7)
Copy the code
tuple
Copy the code
len(t6)
Copy the code
9
Copy the code
len(t7)
Copy the code
3
Copy the code
Repeat tuple elements
t7 * 3 Duplicate 3 times
Copy the code
('python', 'java', 'c', 'python', 'java', 'c', 'python', 'java', 'c')
Copy the code
Add multiple tuples
t6 + t7
Copy the code
(0, 1, 2, 3, 4, 5, 6, 7, 8, 'python', 'java', 'c')
Copy the code
Look at the most value
Size is determined by the ASCII code of the elements in a tuple
max(t6)
Copy the code
8
Copy the code
min(t7)
Copy the code
'c'
Copy the code
t5
Copy the code
(1, 2, (3, 4, 5), 'python')
Copy the code
When Max or min is used, the data types of the elements in the tuple must be the same. Otherwise, an error will be reported:
max(t5)
Copy the code
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-41-f6f6f0f7436f> in <module>
----> 1 max(t5)
TypeError: '>' not supported between instances of 'tuple' and 'int'
Copy the code
Member judgment in
1 in t6
Copy the code
True
Copy the code
10 in t6
Copy the code
False
Copy the code
Traverse the tuples
for i in t6: Print through the elements of a tuple
print(i)
Copy the code
0, 1, 2, 3, 4, 5, 6, 7, 8Copy the code
Modify a tuple?
Do not modify
As we mentioned at the beginning, elements in tuples cannot be modified directly
t7
Copy the code
('python', 'java', 'c')
Copy the code
t7[1]
Copy the code
'java'
Copy the code
We get an error when we try to change Java to JavaScript because tuples themselves cannot be modified:
t7[1] = "JavaScript"
Copy the code
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-47-7885d09b5b02> in <module>
----> 1 t7[1] = "JavaScript"
TypeError: 'tuple' object does not support item assignment
Copy the code
Change it to a list
Because lists can be modified, we can first turn tuples into lists, then modify the elements in the corresponding lists, and then go back
t7
Copy the code
('python', 'java', 'c')
Copy the code
t7_1 = list(t7) # 1. Turn to a list
t7_1
Copy the code
['python', 'java', 'c']
Copy the code
t7_1[1] = "javascript" # 2. Modify elements
t7_1
Copy the code
['python', 'javascript', 'c']
Copy the code
t7_2 = tuple(t7_1) # 3, and then into tuples
t7_2
Copy the code
('python', 'javascript', 'c')
Copy the code
Index and slice
Tuples, like lists, are ordered data types in Python, and there are concepts of use and slicing
Using the index
Use index numbers to access tuple elements
t6.index(0) # the index of element 0
Copy the code
0
Copy the code
t6.index(6) # index number of element 6
Copy the code
6
Copy the code
t6[8] # positive index
Copy the code
8
Copy the code
t6[-4] Negative index number
Copy the code
5
Copy the code
t7.index("java") Check the usage number of the element
Copy the code
1
Copy the code
t7[1] View elements by index number
Copy the code
'java'
Copy the code
Using slice
The rules for using slices in tuples are exactly the same as for lists, and you can refer to the list article to learn.
The following is just a case study of using slicing in tuples:
t6[:7]
Copy the code
(0, 1, 2, 3, 4, 5, 6)
Copy the code
t6[1:8:2]
Copy the code
(1, 3, 5, 7)
Copy the code
t6[:8:2] # Start at the beginning with step 2
Copy the code
(0, 2, 4, 6)
Copy the code
From the last element (index -1) to index -8 (not included), the step is -2
t6[-1: -8: -2]
Copy the code
(8, 6, 4, 2)
Copy the code
t6[-2: -7: -2]
Copy the code
(7, 5, 3)
Copy the code
t6[1:8:3]
Copy the code
(1, 4, 7)
Copy the code
Tuples and list comparisons
The same
- Are ordered data types in Python
- Many of the same operations exist: length, maximum, membership, indexing, slicing, and so on
The difference between
- Lists can be modified directly, not tuples; We can turn tuples into lists and then modify the elements indirectly
- Tuples are faster than lists. If we define a variable and need to iterate over it constantly, using tuples is faster