[Python] Slicing Lists
In addition to accessing individual elements from a list we can use Python's slicing notation to access a subsequence of a list. Consider this list of months,
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
We can slice the third quarter of the year from the months list like this:
>>> q3 = months[6:9]
>>> print(q3)
['July', 'August', 'September']
>>> print(months)
['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
There are a couple of slicing shortcuts that simplify common situations. If you would like to make a slice that begins at the very beginning of the original list, or that ends at the very end of the original list, you can omit the start or end index like this:
>>> first_half = months[:6]
>>> print(first_half)
['January', 'February', 'March', 'April', 'May', 'June']
>>> second_half = months[6:]
>>> print(second_half)
['July', 'August', 'September', 'October', 'November', 'December']
[Python] Slicing Lists的更多相关文章
- Python 列表(Lists)
Python 列表(Lists) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类 ...
- [Python] 03 - Lists, Dictionaries, Tuples, Set
Lists 列表 一.基础知识 定义 >>> sList = list("hello") >>> sList ['h', 'e', 'l', ' ...
- Python列表lists索引关于字符串小纪
看的出'字符串列表'中的空格也是计算在内的
- A few things to remember while coding in Python.
A few things to remember while coding in Python. - 17 May 2012 - UPDATE: There has been much discuss ...
- Python基础知识--Slice(切片)和Comprehensions(生成式)
最近在Youtube的Python视频教程上学习Python相关的基础知识,视频由Corey Schafer制作,讲得十分简单明了,英文发音也比较清晰,几乎都能听懂,是一个不错的Python入门学习的 ...
- python字符串、字符串处理函数及字符串相关操作
python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...
- 学习Python之数据类型
格式化字符串 字符串格式化是一种非常简洁的特性,它能让我们动态更新字符串中的内容.假设我们有从服务器获取的用户信息,并希望根据该信息显示自定义消息,第一个想法是应用字符串连接之类的东西. first_ ...
- 转:python获取linux系统及性能信息
原文:http://amitsaha.github.io/site/notes/articles/python_linux/article.html In this article, we will ...
- 那些年被我坑过的Python——一夫当关 第十三章(堡垒机初步设计)
堡垒机架构 堡垒机的主要作用权限控制和用户行为审计,堡垒机就像一个城堡的大门,城堡里的所有建筑就是你不同的业务系统 , 每个想进入城堡的人都必须经过城堡大门并经过大门守卫的授权,每个进入城堡的人必 ...
随机推荐
- python 统计文件top IP
lines = ''' 1.2.2.3 1.21.29.19.... ''' cnt = {} for line in lines.split(): if line not in cnt: cnt[l ...
- POJ 4007 Flood-it!
题目:http://poj.org/problem?id=4007 思路: (lyd学长的思路) IDA*算法,首先迭代加深限制搜索深度. 可以发现如果当前矩阵中除了左上角的连通块之外,共有M种颜色, ...
- java操作文件创建、删除
java操作文件创建.删除: package test; import java.io.File; import java.io.IOException; import org.slf4j.Logge ...
- powerdesigner里的table背景色是不是可以修改的?
Tools->Display Preferences->Format->Table->Modify->Fill->Fill color:
- exsi的虚拟机加载U盘
1. 添加usb控制器: 2.添加设备
- 文本域内容在div中带换行显示
function ReplaceSeperator(mobiles) { var i; var result = ""; var c; for (i = 0; i < mob ...
- iOS开发—— UIMenuController的使用
UIMenuController的展现需要基于一个View视图,其交互则需要基于其所在View视图的Responder.举例来说,如果一个UIMenuController展现在当前ViewContr ...
- 紫书 例题 10-29 UVa 1642(最优连续子序列)
这类求最优连续子序列的题一般是枚举右端点,然后根据题目要求更新左端点, 一般是nlogn,右端点枚举是n,左端点是logn 难点在于如何更新左端点 用一些例子试一下可以发现 每次加进一个新元素的时候 ...
- Object-C,四则运算计算器
下面是是一个比较复杂的类. 定义一个四则运算计算器Caculator的接口和实现. 在main函数中,让用户输入四则运算表达式,比如a+b,a-b. 最后,在控制台输出结果. 用到的语法:接口.类的定 ...
- Spring Boot学习总结(2)——Spring Boot整合Jsp
怎么使用jsp上面起了疑问,查阅了多方资料,找到过其他人的博客的描述,也找到了spring在github上的给出的例子,看完后稍微改动后成功 整合jsp,于是决定将整合过程记载下来. 无论使用的是那种 ...