在Python中使用 slice()
有时代码会变成一团难以阅读的硬编码切片索引。考虑以下代码,它将学生的详细信息(姓名、电话号码、地址、5 个科目的分数)存储在列表中,一一显示,最后显示平均总分。
Python3
L = [('Student A', 9876543210, 'Address of A', 99, 82, 97, 78, 69),
('Student B', 9999999999, 'Address of B', 90, 83, 87, 72, 67),
('Student C', 8888888888, 'Address of C', 88, 41, 56, 61, 93)]
# Display (name, address, phone no., avg marks) for each student
for student in L:
print(student[0:3], "Marks:", sum(student[3:])/5)
# Average total marks of the class
sum_of_marks = 0
for student in L:
sum_of_marks += sum(student[3:])
print(sum_of_marks / 3)
Python3
l = [('Student A', 9876543210, 'Address of A', 99, 82, 97, 78, 69),
('Student B', 9999999999, 'Address of B', 90, 83, 87, 72, 67),
('Student C', 8888888888, 'Address of C', 88, 41, 56, 61, 93)]
# Naming slice
details = slice(0, 3)
marks = slice(3, 8)
# display (name, address, phone no., avg marks) for each student
for student in l:
print(student[details], "MArks:", sum(student[marks])/5)
# display average total marks of the class
sum_of_marks = 0
for student in l:
sum_of_marks += sum(student[marks])
print(sum_of_marks / 3)
输出:
('Student A', 9876543210, 'Address of A') Marks: 85.0
('Student B', 9999999999, 'Address of B') Marks: 79.8
('Student C', 8888888888, 'Address of C') Marks: 67.8
387.6666666666667
上面的代码完成了它的工作,但是这个代码可能出现的一个问题是,如果将来我们决定在索引 1 处添加一个条目“Roll no”。那么整个代码将需要更改。此外,这段代码的可读性也不太好。这只是一个小例子。在大型项目中,我们编写了数千行代码。因此,编写可读和可维护的代码成为我们的职责。
通过使用slice()构造函数命名切片,可以使相同的代码在一定程度上具有可读性和可维护性。
Syntax:
- slice(stop)
- slice(start, stop, step)
Parameters:
start: Starting index where the slicing of object starts.
stop: Ending index where the slicing of object stops.
step: It is an optional argument that determines the increment between each index for slicing.
Return Type: Returns a sliced object containing elements in the given range only.
注意: slice(1, 5, 2) 对应 a[1:5:2],slice(1, 4) 对应 a[1:4]。
让我们重写我们的代码。
Python3
l = [('Student A', 9876543210, 'Address of A', 99, 82, 97, 78, 69),
('Student B', 9999999999, 'Address of B', 90, 83, 87, 72, 67),
('Student C', 8888888888, 'Address of C', 88, 41, 56, 61, 93)]
# Naming slice
details = slice(0, 3)
marks = slice(3, 8)
# display (name, address, phone no., avg marks) for each student
for student in l:
print(student[details], "MArks:", sum(student[marks])/5)
# display average total marks of the class
sum_of_marks = 0
for student in l:
sum_of_marks += sum(student[marks])
print(sum_of_marks / 3)
输出:
('Student A', 9876543210, 'Address of A') MArks: 85.0
('Student B', 9999999999, 'Address of B') MArks: 79.8
('Student C', 8888888888, 'Address of C') MArks: 67.8
387.6666666666667