2025-09-10:仓库迁移
This commit is contained in:
18
1.数据结构与算法/11.切片命名.py
Normal file
18
1.数据结构与算法/11.切片命名.py
Normal file
@@ -0,0 +1,18 @@
|
||||
items = [0,1,2,3,4,5,6]
|
||||
|
||||
# 我们可以使用内置的slice函数对切片进行命名,参数分别为start|stop|step
|
||||
a = slice(2,6,2)
|
||||
|
||||
print(items[a])
|
||||
print(a.start, a.stop, a.step)
|
||||
|
||||
# indeces方法可以帮我们把start和stop自动限制在序列长度内,并返回一个限制后的切片
|
||||
s = "sukahelloworld"
|
||||
# 上面那个可能看着不明显,但是下面这个看着就明显多了
|
||||
# s = "suka"
|
||||
|
||||
print(a.indices(len(s)))
|
||||
|
||||
# 因为返回的是元组,所以要使用得先解包
|
||||
for i in range(*a.indices(len(s))):
|
||||
print(s[i])
|
Reference in New Issue
Block a user