一文明白 python的切片
<div style="color: black; text-align: left; margin-bottom: 10px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://p3-sign.toutiaoimg.com/3f2a000401d88d40acde~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723896136&x-signature=d74gsZt0JEw%2BmPdhPV9gDIZAN50%3D" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">取一个list或tuple的部分元素是非常<span style="color: black;">平常</span>的操作。<span style="color: black;">例如</span>,一个list如下:</p>>>> L = <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">取前3个元素,应该怎么做?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">笨办法:</p>>>> , L, L]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">之<span style="color: black;">因此</span>是笨办法是<span style="color: black;">由于</span>扩展一下,取前N个元素就没辙了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">取前N个元素,<span style="color: black;">亦</span><span style="color: black;">便是</span>索引为0-(N-1)的元素,<span style="color: black;">能够</span>用循环:</p>>>> r = []>>> n = 3>>> for i in range(n):... r.append(L)... >>> r
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对这种经常取指定索引范围的操作,用循环<span style="color: black;">非常</span>繁琐,<span style="color: black;">因此呢</span>,Python<span style="color: black;">供给</span>了切片(Slice)操作符,能大大简化这种操作。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对应上面的问题,取前3个元素,用一行代码就<span style="color: black;">能够</span>完成切片:</p>>>> L<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">L<span style="color: black;">暗示</span>,从索引0<span style="color: black;">起始</span>取,直到索引3为止,但不<span style="color: black;">包含</span>索引3。即索引0,1,2,正好是3个元素。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span><span style="color: black;">第1</span>个索引是0,还<span style="color: black;">能够</span>省略:</p>>>> L[:3]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">亦</span><span style="color: black;">能够</span>从索引1<span style="color: black;">起始</span>,取出2个元素出来:</p>>>> L<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">类似的,既然Python支持L[-1]取倒数<span style="color: black;">第1</span>个元素,<span style="color: black;">那样</span>它<span style="color: black;">一样</span>支持倒数切片,试试:</p>>>> L[-2:]>>> L[-2:-1]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">记住倒数<span style="color: black;">第1</span>个元素的索引是-1。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">切片操作<span style="color: black;">非常</span>有用。<span style="color: black;">咱们</span>先创建一个0-99的数列:</p>>>> L = list(range(100))>>> L
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span><span style="color: black;">经过</span>切片<span style="color: black;">容易</span>取出某一段数列。<span style="color: black;">例如</span>前10个数:</p>>>> L[:10]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后10个数:</p>>>> L[-10:]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前11-20个数:</p>>>> L<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前10个数,每两个取一个:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://p3-sign.toutiaoimg.com/3f2d0001d96d303a1748~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1723896136&x-signature=U2BkBSO4xBheE5Q3gRPI1FBI2Y0%3D" style="width: 50%; margin-bottom: 20px;"></p>>>> L[:10:2]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">所有数,每5个取一个:</p>>>> L[::5]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">乃至</span>什么都不写,只写</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[:]</p>就<span style="color: black;">能够</span>原样复制一个list:>>> L[:]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">tuple<span style="color: black;">亦</span>是一种list,<span style="color: black;">独一</span>区别是tuple不可变。<span style="color: black;">因此呢</span>,tuple<span style="color: black;">亦</span><span style="color: black;">能够</span>用切片操作,只是操作的结果仍是tuple:</p>>>> (0, 1, 2, 3, 4, 5)[:3](0, 1, 2)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">字符串</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">xxx</p><span style="color: black;">亦</span><span style="color: black;">能够</span>看成是一种list,<span style="color: black;">每一个</span>元素<span style="color: black;">便是</span>一个字符。<span style="color: black;">因此呢</span>,字符串<span style="color: black;">亦</span><span style="color: black;">能够</span>用切片操作,只是操作结果仍是字符串:>>> ABCDEFG[:3]ABC>>> ABCDEFG[::2]ACEG<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">非常多</span>编程语言中,针对字符串<span style="color: black;">供给</span>了<span style="color: black;">非常多</span><span style="color: black;">各样</span>截取函数(例如,substring),其实目的<span style="color: black;">便是</span>对字符串切片。Python<span style="color: black;">无</span>针对字符串的截取函数,只<span style="color: black;">必须</span>切片一个操作就<span style="color: black;">能够</span>完成,非常简单。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">小结</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了切片操作,<span style="color: black;">非常多</span><span style="color: black;">地区</span>循环就<span style="color: black;">再也不</span><span style="color: black;">必须</span>了。Python的切片非常灵活,一行代码就<span style="color: black;">能够</span>实现<span style="color: black;">非常多</span>行循环<span style="color: black;">才可</span>完成的操作。</p>
</div>
交流如星光璀璨,点亮思想夜空。 你的留言真是温暖如春,让我感受到了无尽的支持与鼓励。 认真阅读了楼主的帖子,非常有益。 我赞同你的看法,你的智慧让人佩服,谢谢分享。
页:
[1]