大家好,又见面了,我是你们的朋友全栈君。
# 数据源
names = ['a', 'b', 'c', 'd', 'e', 'f']
# 正常获取
print(names[1]) # b
# 截取
print(names[0:3]) # ['a', 'b', 'c']
print(names[3:10]) # ['d', 'e', 'f']
# 截取:从指定开始直接到指定结尾
print(names[3:]) # ['d', 'e', 'f']
print(names[:3]) # ['a', 'b', 'c']
# 倒着切
print(names[-2: -1]) # ['e']
print(names[-2:]) # ['e', 'f']
# 跳着切,步长
print(names[0: 5: 2]) # ['a', 'c', 'e']
print(names[0::2]) # ['a', 'c', 'e']
print(names[::2]) # ['a', 'c', 'e']
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/132594.html原文链接:https://javaforall.cn