目录

Sequence Overview

序列中可以包含序列

Common Sequence Operations

Indexing

Slicing

A Nifty Shortcut

Longer Steps


Sequence Overview

The main difference between lists and tuples is that you can change a list, but you can’t change a tuple.

This means a list might be useful if you need to add elements as you go along, while a tuple can be useful if,
for some reason, you can’t allow the sequence to change. Reasons for the latter are usually rather technical,
having to do with how things work internally in Python. That’s why you may see built-in functions returning
tuples. For your own programs, chances are you can use lists instead of tuples in almost all circumstances.

序列中可以包含序列

sequences can contain other sequences, too, so you could make a list of such persons, which would be
your database.

>>> edward = ['Edward Gumby', 42]
>>> john = ['John Smith', 50]>>> database = [edward, john]
>>> database
[['Edward Gumby', 42], ['John Smith', 50]]

python has a basic notion of a kind of data structure called a container, which is basically any object
that can contain other objects. the two main kinds of containers are sequences (such as lists and tuples) and
mappings (such as dictionaries). While the elements of a sequence are numbered, each element in a mapping
has a name (also called a key).

Common Sequence Operations

These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.

Indexing

When you use a negative index, Python counts from the right, that is, from the last element. The last element
is at position –1.

>>> greeting = 'Hello'
>>> greeting[0]
'H'>>> greeting[-1]
'o

This is called indexing.

Slicing

Just as you use indexing to access individual elements, you can use slicing to access ranges of elements. You
do this by using two indices, separated by a colon.

The first index is the number of the first element you want to include. However, the last index is the number
of the first element after your slice. Consider the following:

>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[3:6] [4, 5, 6]
>>> numbers[0:1] [1]

前闭后开

In short, you supply two indices as limits for your slice, where the first is inclusive and the second is exclusive.

A Nifty Shortcut

当切片的结束的索引后面再没有元素时,则会把未尾的元素取出来

>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[8:10]
[9, 10]
>>> numbers[8:11]
[9, 10]
>>> numbers[8:9]
[9]>>> num = numbers
>>> num[-3:0]
[]>>> num[-3:]
[8, 9, 10]

extracts the domain name

# Split up a URL of the form http://www.something.com
url = input('Please enter the URL:')
domain = url[11:-4]
print("Domain name: " + domain)
Here is a sample run of the program:
Please enter the URL: http://www.python.org
Domain name: python

Longer Steps

步长参数默认情况下为1,可以设置为其它参数

When slicing, you specify (either explicitly or implicitly) the start and end points of the slice. Another parameter,
which normally is left implicit, is the step length. In a regular slice, the step length is one, which means that the
slice “moves” from one element to the next, returning all the elements between the start and end.

>>> numbers[0:10:1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> numbers[0:10:2]
[1, 3, 5, 7, 9]
numbers[3:6:3]
[4]

You can still use the shortcuts mentioned earlier. For example, if you want every fourth element of a
sequence, you need to supply only a step size of four

>>> numbers[::4]
[1, 5, 9]

Naturally, the step size can’t be zero—that wouldn’t get you anywhere—but it can be negative, which means
extracting the elements from right to left.

>>> numbers[8:3:-1]
[9, 8, 7, 6, 5]
>>> numbers[10:0:-2]
[10, 8, 6, 4, 2]
>>> numbers[0:10:-2]
[]
>>> numbers[::-2]
[10, 8, 6, 4, 2]
>>> numbers[5::-2]
[6, 4, 2]
>>> numbers[:5:-2]
[10, 8]
>>> numbers[::-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Getting things right here can involve a bit of thinking. As you can see, the first limit (the leftmost) is still
inclusive, while the second (the rightmost) is exclusive. When using a negative step size, you need to have a
first limit (start index) that is higher than the second one. What may be a bit confusing is that when you leave
the start and end indices implicit, Python does the “right thing”—for a positive step size, it moves from the
beginning toward the end, and for a negative step size, it moves from the end toward the beginning.

Beginning Python chapter 2 Lists and Tuples:1 Indexing and slicing相关推荐

  1. Think Python - Chapter 12 Tuples

    12.1 Tuples are immutable(元组是不可变的) A tuple is a sequence of values. The values can be any type, and ...

  2. python读取第二行_使用Python操作Excel(二):读取数据表

    上一节我们提到,使用openpyxl可以方便的对数据表进行操作,例如:抽象Excel数据并存入数据库 将数据库数据导出到Excel 给一个已存在的数据表追加信息 我们还介绍了一些Excel的基本术语, ...

  3. Python自然语言处理学习笔记(30):4.2 序列

    4.2   Sequences 序列 So far, we have seen two kinds of sequence object: strings and lists. Another kin ...

  4. python dlib学习(三):调用cnn人脸检测

    前言 调用训练好的卷积神经网络(CNN)模型进行人脸检测. 模型下载链接:http://dlib.net/files/mmod_human_face_detector.dat.bz2 程序 注:使用了 ...

  5. python爬取系统_python应用:爬虫框架Scrapy系统学习第四篇——scrapy爬取笔趣阁小说...

    使用cmd创建一个scrapy项目: scrapy startproject project_name (project_name 必须以字母开头,只能包含字母.数字以及下划线) 项目目录层级如下: ...

  6. 【Python自然语言处理】读书笔记:第四章:编写结构化程序

    4 编写结构化程序 4.1 回到基础 1.赋值: 列表赋值是"引用",改变其中一个,其他都会改变 foo = ["1", "2"] bar ...

  7. Beginning Python PDF 分享

    链接:https://pan.baidu.com/s/11ojPYBn-_RZ5dPCHyvOJbA            eb6e 相关推荐 python基础教程 Python语言入门 Python ...

  8. python读书报告_Python读书笔记:细节决定成败(1)

    背景:我本来是一个信奉Java大法好的程序员.但是最近由于工作原因,不得不开始学习python.因此,写下这个读书笔记,希望能起到一个抛砖引玉的作用.原文中所有引用部分均来自python官方的tuto ...

  9. Python时间序列模型推理预测实战:时序推理数据预处理(特征生成、lstm输入结构组织)、模型加载、模型预测结果保存、条件判断模型循环运行

    Python时间序列模型推理预测实战:时序推理数据预处理(特征生成.lstm输入结构组织).模型加载.模型预测结果保存.条件判断模型循环运行 目录

  10. Python使用pandas保存csv文件:如果文件存在则只添加内容(append),如果无表则同时写入表头和内容(write)

    Python使用pandas保存csv文件:如果文件存在则只添加内容(append),如果无表则同时写入表头和内容(write) 目录

最新文章

  1. pringMVC“Ambiguous mapping found. Cannot map ‘XXXController‘ bean method”解决方法
  2. Catalyst3560密码破解
  3. user-agent
  4. 【tomcat】手动部署动态JavaWeb项目到tomcat
  5. 《Spring 3.0就这么简单》——1.6 展现层
  6. (转)git常用命令
  7. npm run dev 和 npx webpack-dev-server
  8. Android:自定义标题栏
  9. CentOS卸载自带的JDK
  10. Oracle 存储过程、存储函数 与原生 JDBC 调用
  11. 设计模式详解(链接)
  12. php laravel 面试,当面试关问你Laravel Facade,说出这几个关键词就可以
  13. Android开发简易计算器
  14. Coap协议(1)入门简介
  15. boost电路输出电流公式_开关电源BOOST拓扑计算公式和参考分析
  16. GAMES101-现代计算机图形学入门-闫令琪——Lecture 06 Rasterization 2 (Antialiasing and Z-Buffering) 学习笔记
  17. 安卓java模拟器怎么用_安卓java模拟器(安卓手机如何玩JAVA游戏以及JAVA软件的方法)...
  18. rs485数据线接反_RS485引脚说明及接口说明
  19. App、H5、PC应用多端开发框架Flutter 2发布
  20. 微信小程序(五)新版的用户授权和判断是否是否已经授权和自动提示更新版本

热门文章

  1. 世界杯:左撇子在体育方面大有前途
  2. linux 内核 介绍,Linux内核详细介绍
  3. java输入任意一个字母的语句_java-检查用户输入的字符串是否包含用户输入的字母(不包括任何多余字母)的if语句...
  4. python pandas series想赋予新的值_Python-pandas根据其他列的值创建新列/逐行应用多列的功能...
  5. hive xmlserde_各种数据格式的Hive建表语句
  6. 浮点错误是什么意思_Excel函数计算常见错误值,都是什么意思
  7. java 静态库和动态库_Android下Java的静态库和动态库
  8. 三朵云 华为_【创业前沿】华为突然传来大消息!对不起,我要辞职了!
  9. TransactNamedPipe函数
  10. 7. COM编程——初始化并创建COM对象