python 切片_通过Python示例了解切片符号

python 切片

Syntaxes to use slice notation

使用切片符号的语法

    object[begin:end]   # starts with begin, stops at end-1
    object[end:]        # start with begin, and rest of the items
    object[:stop]       # starts from 0, stops at end-1
    object[:]           # returns a copy of whole array
    object[begin:end:step]  # starts with begin, stops at end-1 with given step

理解切片符号的Python程序 (Python program for understanding slice notation)

x = [10, 20, 30, 40, 50] # list
y = (10, 20, 30, 40, 50) # tuple
z = "https://www.includehelp.com/" # string

print("x =", x)
print("y =", y)
print("z =", z)
print()

print("x[0:5] =", x[0:5])
print("x[0:4] =", x[0:4])
print("x[2:5] =", x[2:5])
print("x[0:] =", x[0:])
print("x[:5] =", x[:5])
print("x[:3] =", x[:3])
print("x[0:5:2] =", x[0:5:2])
print()

print("y[0:5] =", y[0:5])
print("y[0:4] =", y[0:4])
print("y[2:5] =", y[2:5])
print("y[0:] =", y[0:])
print("y[:5] =", y[:5])
print("y[:3] =", y[:3])
print("y[0:5:2] =", y[0:5:2])
print()

print("z[0:5] =", z[0:5])
print("z[0:4] =", z[0:4])
print("z[2:5] =", z[2:5])
print("z[0:] =", z[0:])
print("z[:5] =", z[:5])
print("z[:3] =", z[:3])
print("z[0:5:2] =", z[0:5:2])
print()

Output

输出量

x = [10, 20, 30, 40, 50]
y = (10, 20, 30, 40, 50)
z = https://www.includehelp.com/

x[0:5] = [10, 20, 30, 40, 50]
x[0:4] = [10, 20, 30, 40]
x[2:5] = [30, 40, 50]
x[0:] = [10, 20, 30, 40, 50]
x[:5] = [10, 20, 30, 40, 50]
x[:3] = [10, 20, 30]
x[0:5:2] = [10, 30, 50]

y[0:5] = (10, 20, 30, 40, 50)
y[0:4] = (10, 20, 30, 40)
y[2:5] = (30, 40, 50)
y[0:] = (10, 20, 30, 40, 50)
y[:5] = (10, 20, 30, 40, 50)
y[:3] = (10, 20, 30)
y[0:5:2] = (10, 30, 50)

z[0:5] = https
z[0:4] = http
z[2:5] = tps
z[0:] = https://www.includehelp.com/
z[:5] = https
z[:3] = htt
z[0:5:2] = hts


翻译自: https://www.includehelp.com/python/understanding-slice-notation-with-examples-in-python.aspx

python 切片

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值