序列包括字符串,列表,元祖,序列中的每个元素都有编号,其中只有元祖不能修改
 
通用序列操作包括索引、 切片、 相加、 相乘和成员资格检查
 
索引
>>> greeting = 'Hello'
>>> greeting[0]
'H'
>>> greeting[-1]
'o'
>>> 'Hello'[1]
'e'
>>> fourth = input('Year: ')[3]
Year: 2005
>>> fourth
'5'
 
切片
>>> tag = '<a href="http://www.python.org">Python web site</a>'
>>> tag[9:30]
>>> tag[32:-4]
'Python web site'
>>> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> numbers[3:6] [4, 5, 6]
>>> numbers[0:1] [1]
开始元素包括结尾元素不包括
>>> numbers[-3:-1]
[8, 9]
>>> numbers[-3:0]
【】
>>> numbers[-3:]
[8, 9, 10]
>>> numbers[:3]
[1, 2, 3]
>>> numbers[:]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
第三位为步长
>>> 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]
>>> numbers[::4]
[1, 5, 9]
步长不能为0,否则无法向前移动,但可以为负数,即从右向左提取元素
>>> 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]
 
相加
>>> [1, 2, 3] + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> 'Hello,' + 'world!'
'Hello, world!'
>>> [1, 2, 3] + 'world!'
Traceback (innermost last):
File "<pyshell>", line 1, in ?
[1, 2, 3] + 'world!'
TypeError: can only concatenate list (not "string") to list
 
乘法
>>> sequence = [None] * 10
>>> sequence
[None, None, None, None, None, None, None, None, None, None]
[],[0],[None]都表示空序列
 
成员资格
>>> permissions = 'rw'
>>> 'w' in permissions
True
>>> 'x' in permissions
False
>>> users = ['mlh', 'foo', 'bar']
>>> input('Enter your user name: ') in users
Enter your user name: mlh
True
>>> subject = '$$$ Get rich now!!! $$$'
>>> '$$$' in subject
True
长度,最小值和最大值
>>> numbers = [100, 34, 678]
>>> len(numbers)
3
>>> max(numbers)
678
>>> min(numbers)
34
>>> max(2, 3)
3

Python-2-序列及通用序列操作的更多相关文章

  1. Python通用序列操作

    1.序列概览 1.数据结构 序列.容器 Python中最基本的数据结构是序列,其有索引(从左到右第一个索引为0,从右到左第一个索引为-1). Python包含6中内建的序列: 列表 元组 字符串 Un ...

  2. Python基础--通用序列操作

    Python 继续 Python包含6种内建的序列,各自是:列表.元组.字符串.Unicode字符串.buffer对象和xrange对象.我们将逐步进行介绍. 今天主要介绍一下通用序列操作.放之四海而 ...

  3. Python的6种内建序列之通用操作

    数据结构式通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在Python中,最基本的数据结构是序列(sequence).序列中的每 ...

  4. python数据结构(一)------序列

    数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合:在Python中,最基本的数据结构是序列(sequence),序列中的每个元素被分配一个序列号--即元素的位置,也称为索引. p ...

  5. python教程(四)·序列

    距离上次的小项目已经休息了很长一段时间,是时候来继续本系列教程了.这一节开始我们将深入python中的数据结构. 序列的概念 在python中,最基本的数据结构是序列,序列包含一个或多个元素,每个元素 ...

  6. 孤荷凌寒自学python第三天 初识序列

    孤荷凌寒自学python第三天 初识序列 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python的序列非常让我着迷,之前学习的其它编程语言中没有非常特别关注过序列这种类型的对象,而pyt ...

  7. 《流畅的Python》Data Structures--第2章序列array

    第二部分 Data Structure Chapter2 An Array of Sequences Chapter3 Dictionaries and Sets Chapter4 Text vers ...

  8. python学习笔记(一)元组,序列,字典

    python学习笔记(一)元组,序列,字典

  9. Python——序列封包与序列解包

    一.序列封包与序列解包 把多个值赋给一个变量时,Python会自动的把多个值封装成元组,称为序列封包. 把一个序列(列表.元组.字符串等)直接赋给多个变量,此时会把序列中的各个元素依次赋值给每个变量, ...

随机推荐

  1. Android Touch事件分发

    跟touch事件相关的3个方法: public boolean dispatchTouchEvent(MotionEvent ev); //用来分派event public boolean onInt ...

  2. 解决 eclipse出现 Address already in use: bind

    今天开发遇到下面问题,贴出部分异常信息,如下: [WARNING] failed SelectChannelConnector@ java.net.BindException: Address alr ...

  3. 51nod1934:受限制的排列 (分治+组合数)

    对于一个  11 到  nn 的排列  p1,p2,⋯,pnp1,p2,⋯,pn ,我们可以轻松地对于任意的  1≤i≤n1≤i≤n 计算出  (li,ri)(li,ri) ,使得对于任意的  1≤L ...

  4. 【CQ18高一暑假前挑战赛5】标程

    [A:暴力] #include<bits/stdc++.h> using namespace std; ; int a[maxn],vis[maxn],N,M; int main() { ...

  5. SPOJ:Another Longest Increasing Subsequence Problem(CDQ分治求三维偏序)

    Given a sequence of N pairs of integers, find the length of the longest increasing subsequence of it ...

  6. AOP原理

    AOP(面向切面编程):扩展功能时不修改源代码,采用横向抽取机制 纵向抽取机制: 横向抽取机制: 第一种情况: 第二种情况:

  7. Jmeter 在什么情况下定义多个thread group?

    Jmeter里面有三种线程组:setUp thread group, TearDown thread group, thread group. 如果想定义100个用户登录系统,60个用户做A操作,40 ...

  8. c++ zlib(qt)压缩与解压缩

    #include <QtCore/QCoreApplication> #include "zlib.h" #include "stdio.h" #i ...

  9. json对象和json数组

    json字符串对象和json字符串数组:JSONArray跟JSONObject的区别就是JSONArray比JSONObject多中括号[] jsonObject: "Row": ...

  10. python-format函数

    #通过位置 print '{0},{1}'.format('chuhao',20) print '{},{}'.format('chuhao',20) print '{1},{0},{1}'.form ...