Python入门第9课-切片
<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;">前面讲到了<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>之为切片。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">9.1 切片</strong></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>个元素和最后一个元素的索引,Python在到达指定的第二个索引前面的元素后停止。例如,要输出列表中前三个元素,<span style="color: black;">必须</span>指定索引0-3,这将输出索引分别为0、1、2的元素。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">numbers.py</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① numbers = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② print(numbers)</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>是一个列表,即切片是列表的子集:</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>生成列表的任何子集。例如,想提取列表的第2-4个元素,可将<span style="color: black;">初始</span>索引指定为1,终止索引指定为4:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① numbers = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② print(numbers)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这一次,切片始于 4,<span style="color: black;">最终</span>2:</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;">第1</span>个索引,默认从列表的开头<span style="color: black;">起始</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① numbers = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② print(numbers[:4])</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>索引,默认从索引0<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>指定终止索引,默认终止与列表末尾:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① numbers = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② print(numbers)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Python返回从第2个元素到列表末尾的所有元素:</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>numbers[-3:]:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① numbers = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② print(numbers[-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;">起始</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;"><strong style="color: blue;">9.2 遍历切片</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>要遍历列表的部分元素,可在for循环中<span style="color: black;">运用</span>切片,例如:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># -*- coding:utf-8 -*-</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① numbers = </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② for number in numbers[:3]:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">③ print("您的幸运数字是%d" % number)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第①行 的代码只遍历了列表的前3个数字,<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;">您的幸运数字是1</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">您的幸运数字是4</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">您的幸运数字是7</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>用户的游戏得分,为了取出用户的TOP得分,<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>切片来分页<span style="color: black;">表示</span>信息,一个列表存储100个商品,一页<span style="color: black;">表示</span>10个,<span style="color: black;">第1</span>页<span style="color: black;">表示</span>[:10],第二页<span style="color: black;">表示</span>......。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">9.3 复制列表</strong></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>省略初始和终止索引[:]。</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>想创建一个列表用来存储好基友/好闺蜜最爱吃的:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># -*- coding:utf-8 -*-</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① my_foods = ["Dove","Lindt","Meiji"]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② friend_foods = my_foods[:]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("我最爱吃的<span style="color: black;">食品</span>:")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(my_foods)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("\n我的好<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;">print(friend_foods)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第②行 从列表my_foods中提取了一个切片,<span style="color: black;">由于</span><span style="color: black;">无</span>指定任何索引,从而创建了这个列表的副本,再将这个副本存储到变量friend_foods中。打印<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;">我最爱吃的<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>:</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>列表确实都记录了相应人员<span style="color: black;">爱好</span>的食品:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># -*- coding:utf-8 -*-</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">① my_foods = ["Dove","Lindt","Meiji"]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">② friend_foods = my_foods[:]</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">③ my_foods.append("LeShi")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">④ friend_foods.append("cannoli")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("我最爱吃的<span style="color: black;">食品</span>:")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(my_foods)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("\n我的好<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;">print(friend_foods)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第③行 向列表my_foods中添加了"LeShi";</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">第④行 向列表friend_foods中添加了"cannoli";</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>在正确的列表中:</p>
<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>
<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>:</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>friend_foods = my_foods 呢?<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>下复制列表的<span style="color: black;">状况</span>:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># -*- coding:utf-8 -*-</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">my_foods = ["Dove","Lindt","Meiji"]</p>
<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;">friend_foods = my_foods</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">my_foods.append("LeShi")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">friend_foods.append("cannoli")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("我最爱吃的<span style="color: black;">食品</span>:")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print(my_foods)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">print("\n我的好<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;">print(friend_foods)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>将my_foods赋值给friend_foods,而不是将my_foods的副本存储到friend_foods。这种语法<span style="color: black;">实质</span>上是让Python将新变量friend_foods<span style="color: black;">相关</span>到<span style="color: black;">包括</span>在my_foods的列表,<span style="color: black;">因此呢</span>这两个变量都指向同一个列表(<span style="color: black;">能够</span>理解为这两个变量都指向了同一个列表的内存<span style="color: black;">位置</span>)。<span style="color: black;">因此呢</span>,当将"LeShi"添加到my_foods中时,它<span style="color: black;">亦</span>会出<span style="color: black;">此刻</span>friend_foods中;<span style="color: black;">一样</span>,虽然"cannoli"<span style="color: black;">好似</span>只被添加到了friend_foods中,但它<span style="color: black;">一样</span><span style="color: black;">亦</span>将出<span style="color: black;">此刻</span>my_foods中。</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>想要的结果:</p>
<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>
<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>:</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;"><strong style="color: blue;">9.4 实例练习</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">实例1:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建一个列表,用来存储你最想去旅行的10个<span style="color: black;">地区</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>切片打印列表的前3个元素;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>切片打印列表的最后3个元素;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>切片打印中间的3个元素。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">实例2:</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建实例1中列表的副本,用来存储你好友最想去的旅行目的地(假设你想去的<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;">在好友的列表中添加一个新的目的地;</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>for循环分别打印列表,<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;">欢迎关注:<strong style="color: blue;">Python公开课,</strong><span style="color: black;">一块</span>创造有趣的事儿</p>
</div>
一看到楼主的气势,我就觉得楼主同在社区里灌水。 “沙发”(SF,第一个回帖的人) 你的言辞如同繁星闪烁,点亮了我心中的夜空。 论坛是一个舞台,让我们在这里尽情的释放自己。
页:
[1]