序列基础

序列:python包含6种内建的序列,常用的有:列表、元组、字符串。列表可以修改,元组和字符串不能修改。
 索引:从0开始递增,通过索引获取元素;可使用负数索引,从右至左。最后1个元素的位置编号为-1;

>>> s = 'hello'
>>> print(s[-1])
o
>>> print(s[5])
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print(s[5])
IndexError: string index out of range
>>> fourth = input('Year: ')[3]
Year: 2014
>>> print(fourth)
4
>>>  

切片:访问一定范围内的元素,返回新的序列,原序列不变;通过冒号来隔开两个索引。
【左索引:右索引:步长】 左索引不能比右索引晚出现。不包括右边的索引元素,步长默认为1,可以为负数即从右到左提取元素。

>>> s1 = 'hello,world'
>>> print(s[0:5])
hello
>>> num = [1,2,3,4,5,6,7,8,9]
>>> print(num[:])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> print(num[::2])
[1, 3, 5, 7, 9]
>>> print(num[1::2])
[2, 4, 6, 8]
>>> print(num[::-1])
[9, 8, 7, 6, 5, 4, 3, 2, 1]
>>>

序列相加:相同类型的序列之间的操作

>>> print('hello '+'world!')
hello world!
>>> print([1,2,3]+[4,5,6])
[1, 2, 3, 4, 5, 6]
>>> print([1,2,3]+['a','b','c'])
[1, 2, 3, 'a', 'b', 'c']

乘法:

>>> print('*'*10)
**********
>>> print([1,2,3]*10)
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> print((1,2,3)*10)
(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3) 

空列表:None是一个python内建值,表示空。用于构建空列表及其初始化

>>> lst = [None]*10
>>> print(lst)
[None, None, None, None, None, None, None, None, None, None]

成员资格:in 若为真返回True,为假返回False

>>> permissions = 'rw'
>>> print('ro' in permissions)
False
>>> subject = '$$$ Get rich now!!! $$$'
>>> print('$$$' not in subject)
False >>> database = [
['zyj','a'],
['sl','b']
]
>>> username = input("user: ")
user: zyj
>>> password = input('pwd: ')
pwd: a
>>> if [username,password] in database:
print('login sucess!') login sucess!

长度、最小值、最大值:len、min、max

>>> num1 = [100,300,20]
>>> num2 = [1,2,3]
>>> t = (1,2,3)
>>> print(len(num1))
3
>>> print(min(num1))
20
>>> print(min(num1,num2))
[1, 2, 3]
>>> print(max(num1))
300
>>> print(max(t))
3
>>>

list函数:适用于所有类型的序列;生成一个列表。

>>> lst1 = list("hello")
>>> print(lst1)
['h', 'e', 'l', 'l', 'o']
>>> lst2 = list("12345")
>>> print(lst2)
['1', '2', '3', '4', '5']

基本的列表操作:改变列表、删除元素
元素赋值,不能为不存在的元素进行赋值

>>> num3 = [1,5,6]
>>> num3[1] = 2
>>> print(num3)
[1, 2, 6]
>>>

删除元素:del语句实现

>>> names = ['zyj','sl','zyj','py']
>>> del names[2]
>>> print(names)
['zyj', 'sl', 'py']

分片赋值:

>>> name = list('python')
>>> name[2:]=list('game')
>>> print(name)
['p', 'y', 'g', 'a', 'm', 'e']

通过切片赋值插入新元素:在期望索引位置处赋值

>>> name = [1,5]
>>> name[1:1] = [2,3,4]
>>> print(name)
[1, 2, 3, 4, 5]
>>>

通过切片赋值来删除元素:将期望删除的位置赋值为空

>>> name = [1,2,3,4,5]
>>> name[1:4] = []
>>> print(name)
[1, 5]
>>>

上述操作中。当步长不为1时,不能进行插入操作,不能进行非等长赋值。

 列表方法

方法是一个与某些对象有紧密联系的函数, 通过对象.方法(参数)来调用
 append :在列表末尾追加新的对象

>>> lst = list('hello'+'123')
>>> tup = tuple('hello')
>>> lst.append(tup)
>>> print(lst)
['h', 'e', 'l', 'l', 'o', '1', '2', '3', ('h', 'e', 'l', 'l', 'o')]
>>> 

count 统计某个元素在列表中出现的次数

>>> lst = list('hello'*3)
>>> print(lst.count('h'))
3

extend:在列表末尾一次性追加另一个序列中的多个值

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a.extend(b)
>>> print(a)
[1, 2, 3, 4, 5, 6]
>>> a.append(b)
>>> print(a)
[1, 2, 3, 4, 5, 6, [4, 5, 6]]

与连接操作的区别:a + b 不会改变a的值,而extend会改变a的值,生成一个新的列表

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> print(a + b)
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]
>>>

通过切片赋值实现extend的方法:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> a[len(a):] = b
>>> print(a)
[1, 2, 3, 4, 5, 6]
>>>

index方法:从列表中找出某个值第一个匹配项的索引位置,若没有找到则引发异常

>>> lst = list('hello'+'1234')
>>> print(lst.index('1'))
5
>>>

insert方法:用于将对象插入到列表中

>>> lst = [1,2,3,'hello']
>>> lst.insert(len(lst),4)
>>> print(lst)
[1, 2, 3, 'hello', 4]

同样可以使用切片赋值来实现insert方法:

>>> lst = [1,2,3,'hello']
>>> lst[4:4] = [4]
>>> print(lst)
[1, 2, 3, 'hello', 4]

pop方法:移除列表中的一个元素(默认最后一个),并且返回该元素的值,POP方法是唯一一个能修改列表又返回元素值(除None)的列表方法

>>> lst = [1,2,3,'hello']
>>> print(lst.pop())
hello

pop方法可以通过参数去掉特定的元素

>>> lst = [1,2,3,'hello']
>>> print(lst.pop(1))
2
>>>

注在python中没有入栈方法,append方法类似与入栈

>>> lst = [1,2,3,'hello']
>>> lst.append(lst.pop())
>>> print(lst)
[1, 2, 3, 'hello']
>>>

remove用于移除列表中某个值得第一个匹配项,移除不存在的值将返回报错。

>>> lst = [1,2,3,4,'hello',4]
>>> lst.remove(4)
>>> print(lst)
[1, 2, 3, 'hello', 4]
>>> lst.remove('he')
Traceback (most recent call last):
File "<pyshell#109>", line 1, in <module>
lst.remove('he')
ValueError: list.remove(x): x not in list
>>> print(lst)
[1, 2, 3, 'hello', 4]
>>>

reverse方法用于将列表中的元素反向存放

>>> lst = [1,2,3,4,5]
>>> lst.reverse()
>>> print(lst)
[5, 4, 3, 2, 1]
>>> lst1 = lst[::-1]
>>> print(lst1)
[1, 2, 3, 4, 5]
>>> print(lst)
[5, 4, 3, 2, 1]
>>>

sort方法用于在原位置对列表进行排序,改变原来的列表,不会返回一个排序好的副本

>>> lst = [10,5,6,2,3,8,6,4]
>>> lst.sort()
>>> print(lst)
[2, 3, 4, 5, 6, 6, 8, 10]
>>>

当用户需要一个排序好的副本,同时要保留原列表不变的操作

>>> lst = [1,4,6,3,2,9]
>>> lst1 = lst[::]
>>> lst1.sort()
>>> print(lst)
[1, 4, 6, 3, 2, 9]
>>> print(lst1)
[1, 2, 3, 4, 6, 9]
>>>

也可以使用sorted函数实现上述需求:

>>> lst =[1,4,6,3,2,9]
>>> lst1 = sorted(lst)
>>> print(lst1)
[1, 2, 3, 4, 6, 9]
>>> print(lst)
[1, 4, 6, 3, 2, 9]
>>>

高级排序:sort方法中使用可选参数:key和reverse

>>> lst = ['hi','hello','you','me']
>>> lst.sort(key = len)
>>> print(lst)
['hi', 'me', 'you', 'hello']
>>> lst = [1,4,6,3,2,9]
>>> lst.sort(reverse = True)
>>> print(lst)
[9, 6, 4, 3, 2, 1]
>>>

元组

元组:不可变序列
元组的创建:圆括号括起来,元素间使用逗号隔开.注意:当元组只有一个值时,也需要写逗号

>>> tup = 1,2,3
>>> print(tup)
(1, 2, 3)
>>> tup1 = ('hello','world')
>>> print(tup1)
('hello', 'world')
>>> tup3 = ()
>>> print(tup3)
()
>>> tup4 =('hello',)
>>> print(tup4)
('hello',)
>>> print(3*(40+2,))
(42, 42, 42)
>>>

tuple函数:以一个序列作为参数,并把它转换为元组

>>> print(tuple('hello'))
('h', 'e', 'l', 'l', 'o')
>>> print(tuple([1,2,3,4]))
(1, 2, 3, 4)
>>> print(tuple((1,2,3)))
(1, 2, 3)
>>>

元组的操作

>>> a = tuple('hello')
>>> print(a[1])
e
>>> print(a[:2])
('h', 'e')
>>> print(a)
('h', 'e', 'l', 'l', 'o')
>>>

  

 

  

python序列的更多相关文章

  1. [Python笔记][第二章Python序列-复杂的数据结构]

    2016/1/27学习内容 第二章 Python序列-复杂的数据结构 堆 import heapq #添加元素进堆 heapq.heappush(heap,n) #小根堆堆顶 heapq.heappo ...

  2. [Python笔记][第二章Python序列-tuple,dict,set]

    2016/1/27学习内容 第二章 Python序列-tuple tuple创建的tips a_tuple=('a',),要这样创建,而不是a_tuple=('a'),后者是一个创建了一个字符 tup ...

  3. [python笔记][第二章Python序列-list]

    2016/1/27学习内容 第二章 Python序列-list list常用操作 list.append(x) list.extend(L) list.insert(index,x) list.rem ...

  4. python学习笔记:python序列

    python序列包括字符串.列表和元组三部分,下面先总的说一下python序列共有的一些操作符和内建函数. 一.python序列 序列类型操作符 标准类型的操作符一般都能适用于所有的序列类型,这里说一 ...

  5. Python序列类型

    Python序列类型 序列:字符.列表.元组 所有序列都支持迭代 序列表示索引为非负整数的有序对象集合 字符和元组属于不可变序列,列表可变 1)字符 字符串字面量:把文本放入单引号.双引号或三引号中: ...

  6. python 序列:字符串、列表、元组

    python 序列:字符串.列表.元组   序列:包含一定顺序排列的对象的一个结构 内建函数:str() list() tuple() 可以使用str(obj)可以把对象obj转换成字符串 list( ...

  7. python序列元素引用容易出错的地方

    python序列分列表和元组,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号.元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可.举个简单的例子,a1是一个元组,a2是一个列表 ...

  8. Python——序列

    #!/usr/bin/python #coding:utf8 ''' Python——序列 字符串的操作 ''' s = 'abcdefg' print s print s[2] print s[-1 ...

  9. python序列和其它类型的比较

    序列对象可以与相同类型的其他对象比较.它们使用 字典顺序 进行比较:首先比较两个python序列的第一个元素,如果不同,那么这就决定了比较操作的结果.如果它们相同,就再比较每个序列的第二个元素,以此类 ...

  10. Python序列及其操作(常见)

    python序列及函数入门认识: 0. 我们根据列表.元组和字符串的共同特点,把它们三统称为什么?    序列,因为他们有以下共同点: 1)都可以通过索引得到每一个元素 2)默认索引值总是从0开始(当 ...

随机推荐

  1. DiskFileItemFactory类的使用

      将请求消息实体中的每一个项目封装成单独的DiskFileItem (FileItem接口的实现) 对象的任务由 org.apache.commons.fileupload.FileItemFact ...

  2. centos6.5安装oracle11g_2

    centos7安装oracle数据库不成功,换成centos6.5安装,可以安装成功,记录一下 安装系统时,主机名如果不是用localhost,安装成功后,要用主机名和ip做映射,修改/etc/hos ...

  3. 在Application中集成Microsoft Translator服务之翻译语言代码

    Microsoft  Translator支持多种语言,当我们获取服务时使用这些代码来表示我们是使用哪种语言翻译成什么语言,以下是相关语言对应的代码和中文名 为了方便我已经将数据库上传到云盘上,读者可 ...

  4. Bootstrap学习------按钮

    Bootstrap为我们提供了按钮组的样式,博主写了几个简单的例子,以后也许用的到. 效果如下 代码如下 <!DOCTYPE html> <html> <head> ...

  5. mysql安装使用笔记

    mysql2008年被sun公司10亿美元收购, 后sun被oracle收购. widenius : 维德纽斯重新写的mysql的分支 mariaDB. 白发程序员, 是由 瑞典mysql AB公司开 ...

  6. visio二次开发——图纸解析

    (转发请注明来源:http://www.cnblogs.com/EminemJK/) visio二次开发的案例或者教程,国内真的非常少,这个项目也是花了不少时间来研究visio的相关知识,困难之所以难 ...

  7. Vundle的安装

    1.Vundle.vim 安装 https://github.com/VundleVim/Vundle.vim 2.插件安装https://github.com/yangyangwithgnu/use ...

  8. Eclipse启动tomcat,http://localhost:8080/无法访问的解决方法

    双击eclipse的Server打开配置页面,将server locations的选项改成第二项,然后把Destroy Path改成webapps,然后就可以了.如果是灰色的,没法进行修改,那么就要先 ...

  9. php之jquery

    <!DOCTYPE html> <html> <head> <script type="xxx.js"></script> ...

  10. R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记(转)

    https://ask.julyedu.com/question/7490 labelImg:https://github.com/tzutalin/labelImg