[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——一夫当关 第十三章(堡垒机初步设计)
堡垒机架构 堡垒机的主要作用权限控制和用户行为审计,堡垒机就像一个城堡的大门,城堡里的所有建筑就是你不同的业务系统 , 每个想进入城堡的人都必须经过城堡大门并经过大门守卫的授权,每个进入城堡的人必 ...
随机推荐
- C++开发人脸性别识别教程(3)——OpenCv配置和ImageWatch插件介绍
OpenCv是C++图像处理的重要工具.这个人脸性别识别的项目就是借助OpenCv进行开发的. 尽管网上已经有了非常多关于OpenCv的配置教程,但出于教程完整性考虑.这里还是用专门的一篇博客来介绍O ...
- php冒泡排序函数
$arr=array(23,5,26,4,9,85,10,2,55,44,21,39,11,16,55,88,421,226,588); function maopao($arr,$value){// ...
- hdoj--1005--Number Sequence(规律题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 关于安装Ubuntu系统时提示“分配到/的分区/dev/sdax开始于xxxx字节,使用磁盘的最小对齐,这可能造成非常差的性能..."的解决办法
由于Windows系统的VMware出了一些问题,索性就不用VM了,直接装上双系统好了.但是在最后安装时出现错误提示“分配到/的分区/dev/sdc1开始于3584字节,使用磁盘的最小对齐,这可能造成 ...
- matplotlib 可视化 —— 移动坐标轴(中心位置)
通常软件绘图,包括 matlab.python 的 matplotlib,默认都是将坐标轴置于画布(figure)的最下侧(x 轴),最左侧(y 轴),也即将坐标原点置于左下角.而我们自己理解数学,以 ...
- rest_framework_HyperlinkedIdentityField
#生成链接 HyperlinkedIdentityField class UserInfoSerializer(serializers.ModelSerializer): group = serial ...
- Struts2的struts.xml的标准配置文档
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...
- fixed说明
http://blog.163.com/hc_ranxu/blog/static/367231822013102265151785/
- 《剑指offer》旋转数组的最小数字
一.题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 二.输入描述 输入一个递增排序的数组的一个旋转 三.输出描述 输出旋转数组的最小元素 例如: 例如数组{3,4,5,1 ...
- hdu 1757 矩阵连乘