python3入门之列表和元组
2015-07-14 16:48
886 查看
获得更多资料欢迎进入我的网站或者 csdn或者博客园
前面一张主要学习了Python的安装,以及第一个程序helloword的编写,以及简单的输入和输出函数序列
这章主要介绍的是列表和元组,而列表和元组是序列的六种内型中的两种,主要区别。列表可以修改,而元组不能。而序列很好用比如要操作一组人的名字和年龄可以这样:>>>peace=['peace one',23] >>>rong=['sisi',22] >>>data=[peace,rong] >>>data [['peace one',23],['sisi',22]]
序列可以包含其他序列;比如data包含了peace和rong序列
索引
1、序列名按序号索引peace[1]
23
data[1][1]
22
2、字符串直接按序号索引还可对输入进行索引
‘hello’[1]
‘e’
two=raw_input(‘year: ‘)[1]
year: 2015
two
‘0’
分片索引、
1跟按序号索引类似,可以使用分片操作来访问一定范围内的元素。分片通过冒号分隔开来实现;tag=’My name is one peace’
tag[11:20]
‘one peace’
注意:第一个索引是分片的第一个元素索引,第二个索引是分片元素最后一个元素索引+1;哪怕像上面的+1索引不存在也没关系.同样制空最后一个索引也是可以的;如下:
tag[-9: ]
‘one peace’
2更大的步长,在两个索引后面再加一个冒号和步长;
tag[1:11:2]
‘ynm s’
注意:同样步长也可以为负数,不过为负数时,是从后往前输出。此时必须第一个索引再第二个索引的后面;比如:
tag[11:1:-2]
‘os mn’
序列运算
1相加又称连接[1,2,3]+[3,4,6,]
[1,2,3,4,5,6]
注意:两种相同类型才能进行连接操作;比如:[1,2]+’hello’会返回错误
2相乘用列表乘以数字x,得到的新的序列会重复被乘序列x次
[42]*5
[42,42,42,42,42]
3成员资格,检查一个值是否在序列当中,可以使用in运算。
‘p’ in tag
True
‘pe’ in tag
True
‘px’ in tag
False
4长度,最大值,最小值
#长度函数len
len(tag)
20
#最大值函数max
max([3,4,5])
5
min([3,4,5])
3
列表
list函数
1,list函数,创建列表;str=list(‘hello’)
str
[‘h’,’e’,’l’,’l’,’o’]
列表操作
1改变列表,元素赋值str[1]=’p’
str
[‘h’,’p’,’l’,’l’,’o’]
2删除元素 使用del来删除
del str[1]
str
[‘h’,’l’,’l’,’o’]
3分片赋值。主要作用如下:
#1,可以使用与原序列不等长的序列将分片替换
str[1: ]=list(‘peace’)
str
[‘h’,’p’,’e’,’a’,’c’,’e’]
#2,可以不替换任可元素的情况下插入元素
str[1:1]=list(‘one’)
str
[‘h’,’o’,’n’,’e’,’p’,’e’,’a’,’c’,’e’]
#3,当然也可以删除、
str[1:4]=list()
str
[‘h’,’p’,’e’,’a’,’c’,’e’]
列表方法
方法是一个与对象紧密相关的函数。直接对象.方法进行调用列表有append(末尾追加),count(统计次数),extend(新列表追加),index(找到元素为知),insert(按序号插入)
pop(按序号删除)remove(按值删除)reverse(元素翻转),sort(排序),sorted(获取排序后的结果),特殊排序:
sort(cmp)通过比较类型排序,sort(key=len)通过建函数排序(此去是长度len),sort(reverse=True)通过True和false来判断是否翻转排序;
方法操作如下:
#append方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.append(list(" tiger")) >>> name ['s', 'c', 'o', 't', 't', [' ', 't', 'i', 'g', 'e', 'r']] >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.append("A","B") #添加多个元素即将报错 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: append() takes exactly one argument (2 given) >>> name.append("A") >>> name ['s', 'c', 'o', 't', 't', 'A'] #count方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.count('s') 1 >>> name.count("t") 2 >>> name.count("A") 0 >>> name.append(list("Python")) >>> name ['s', 'c', 'o', 't', 't', ['P', 'y', 't', 'h', 'o', 'n']] >>> name.count(['P', 'y', 't', 'h', 'o', 'n']) 1 #extend方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.extend(list(" tiger")) >>> name ['s', 'c', 'o', 't', 't', ' ', 't', 'i', 'g', 'e', 'r'] #index方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.index('t') ##第一个字母t的索引位置是3 3 >>> name.index('a') Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: list.index(x): x not in list >>> 'a' in name False >>> 'a' not in name True #insert方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.insert(2,'tiger') ##在索引为2的地方插入字符串tiger >>> name ['s', 'c', 'tiger', 'o', 't', 't'] #pop方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.pop() 't' >>> name ['s', 'c', 'o', 't'] >>> name.append("t") >>> name ['s', 'c', 'o', 't', 't'] #remove方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.remove("t") #去掉第一个t >>> name ['s', 'c', 'o', 't'] >>> name.remove("A") #不存在会报错 Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: list.remove(x): x not in list >>> "A" not in name True >>> name.remove("s","c") #一次只能移除一个元素 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: remove() takes exactly one argument (2 given) #reverse方法 >>> name = list("scott") >>> name ['s', 'c', 'o', 't', 't'] >>> name.reverse() >>> name ['t', 't', 'o', 'c', 's'] #sort方法 >>> result = [8,5,5,3,9] >>> result.sort() >>> result [3, 5, 5, 8, 9] #sorted方法 >>> result = [8,5,5,3,9] >>> result2 = sorted(result) >>> result [8, 5, 5, 3, 9] >>> result2 [3, 5, 5, 8, 9]
元组
元组元组与列表一样,也是一种序列,唯一不同的是元组不可以修改:
元组操作
>>> 1,2,3 (1, 2, 3) >>> () () #对于单个元素必须加上逗号,加上逗号后就表示数字是元组了 >>> 42 42 >>> 42, (42,) >>> (42,) (42,)
元组tuple函数
>>> tuple([1,2,3]) (1, 2, 3) >>> tuple('abc') ('a', 'b', 'c') >>> tuple((1,2,3)) (1, 2, 3)
列表与元组的相互转化
>>> T=('cc','aa','dd','bb') >>> tmp=list(T) >>> tmp ['cc', 'aa', 'dd', 'bb'] >>> T=tuple(tmp) >>> T ('cc', 'aa', 'dd', 'bb')
相关链接:
python3入门之类python3入门之函数
python3入门之循环
python3之if语句
python3入门之赋值语句介绍
python3入门之print,import,input介绍
python3入门之set
python3入门之字典
python3入门之字符串
python3入门之列表和元组
python3入门之软件安装
python3爬虫之入门和正则表达式
相关文章推荐
- Python3入门之软件安装
- python3入门之列表和元组
- Python之 sort和sorted
- Python3入门之软件安装
- Python入门
- python爬虫程序所需的工具整理
- 使用python的smtp模块发送邮件
- 通过字典-python实现用户信息查询
- Python.resource-for-python-from-internet
- Python实现一个简易的网页抓取程序
- Python中subprocess学习
- Ubuntu-安装-Python包
- Python 之字节转换
- python处理跨平台应用
- Python开发入门与实战7-Django Form
- numpy 保存数据
- python 的日志logging模块学习
- IDAPython 脚本
- Python开发入门与实战6-表单
- python语言优点&缺点