首页 > 编程语言 > 详细

[Python]数组切片

时间:2020-05-24 01:01:13      阅读:68      评论:0      收藏:0      [点我收藏+]

来源:https://stackoverflow.com/questions/509211/understanding-slice-notation

 

步长为正数

seq[:]                # [seq[0],   seq[1],          ..., seq[-1]    ]
seq[low:]             # [seq[low], seq[low+1],      ..., seq[-1]    ]
seq[:high]            # [seq[0],   seq[1],          ..., seq[high-1]]
seq[low:high]         # [seq[low], seq[low+1],      ..., seq[high-1]]
seq[::stride]         # [seq[0],   seq[stride],     ..., seq[-1]    ]
seq[low::stride]      # [seq[low], seq[low+stride], ..., seq[-1]    ]
seq[:high:stride]     # [seq[0],   seq[stride],     ..., seq[high-1]]
seq[low:high:stride]  # [seq[low], seq[low+stride], ..., seq[high-1]]

 

步长为负数

seq[::-stride]        # [seq[-1],   seq[-1-stride],   ..., seq[0]    ]
seq[high::-stride]    # [seq[high], seq[high-stride], ..., seq[0]    ]
seq[:low:-stride]     # [seq[-1],   seq[-1-stride],   ..., seq[low+1]]
seq[high:low:-stride] # [seq[high], seq[high-stride], ..., seq[low+1]]

 

a[:]     # a copy of the whole array
a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items
a[::-1]  # all items in the array, reversed

 

[Python]数组切片

原文:https://www.cnblogs.com/hcbin/p/12945289.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!