第二章-列表和元组——python基础教程(第二版)笔记

实际上是通过编号对数组进行引用,第四章的字典则是通过名字引用

2.1序列概述

列表可以修改
元组不能修改

2.2通用序列操作

2.2.1索引

按编号获取元素

test="hello world"
print test[0] #获取第1个元素
print test[-1]#获取倒数第一个元素

输出结果

h
d
Press any key to continue . . .

2.2.2分片

test="hello world"
print test[0:3] #获取第1到第4个元素
print test[1:-1]#获取第二到倒数第一个元素
print test[1:]#获取第二到最后一个元素

#更大的步长
print test[1:-1:2]#获取第二到倒数第一个元素,步长为2

输出结果

hel
ello worl
ello world
el ol
Press any key to continue . . .

2.2.3 序列相加

print [1,2,3]+[4,5,6]
print "hello"+"world"
#列表和字符串不能相加

输出结果

[1, 2, 3, 4, 5, 6]
helloworld
Press any key to continue . . .

2.2.4乘法

print [1,2,3]*5
print "hello"*5

输出结果

[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
hellohellohellohellohello
Press any key to continue . . .

2.2.5成员资格

x="abcdefg"
print "a" in x
print "x"in x

输出结果

True
False
Press any key to continue . . .

2.2.6长度,最小值,最大值

x=[1,2,3,4]
print len(x)  #长度
print max(x)  #最大值
print min(x)  #最小值

输出结果

4
4
1
Press any key to continue . . .

2.3列表:Python的苦力

2.3.1list函数

print list("hello")  #拆分字符串

输出结果

['h', 'e', 'l', 'l', 'o']
Press any key to continue . . .

2.3.2基本的列表操作

#1.改变列表:元素赋值
x=[1,2,3]
x[1]=4   #改第二元素为4
print x

#2.删除元素
x=["a","b","c"]
del x[2]  #删除第三个元素
print x

#3.分片赋值
x=["a","b","c"]
x[1:]=list("asd")   #修改第二道最后的元素为元组 asd
print x
x[1:1]=list("asd")   #相当于插入
print x
x[1:]=list("")   #相当于删除
print x

输出结果

[1, 4, 3]
['a', 'b']
['a', 'a', 's', 'd']
['a', 'a', 's', 'd', 'a', 's', 'd']
['a']
Press any key to continue . . .

2.3.3列表方法

#1.append末尾追加
x=[1,2,3]
x.append(2)
print x

#2.count统计某个元素的词数
x=[1,2,3,4,2,12,3,1,1,1]
print x.count(1)
x=[1,[2,3],4,2,2,3,1,1,1]
print x.count(2)
print x.count([2,3])

#3.extend扩展列表
x=[1,2,3]
y=[4,5]
x.extend(y)
print x

#4.index找到某一项第一个匹配项的索引位置
x=[1,2,3,4,2,12,3,1,1,1]
print x.index(4)

#5.insert插入
x=[1,2,3,4,2,12,3,1,1,1]
x.insert(3,"as")
print x

#6.pop移除列表中的一个元素
x=[1,2,3,4,2,12,3,1,1,2]
print x.pop()
print x

#7.remove移除某个值的第一个匹配项
x=[1,2,3,4,2,12,3,1,1,2]
x.remove(3)
print x

#8.reverse反向存放
x=[1,2,3,4,2,12,3,1,1,2]
x.reverse()
print x

#9.sort简单排序
x=[1,2,3,4,2,12,3,1,1,2]
x.sort()
print x
y=sorted(x) #使y获得x的排序
print y

#高级排序compare
print cmp(100,0)
print cmp(1,2)

输出结果

[1, 2, 3, 2]
4
2
1
[1, 2, 3, 4, 5]
3
[1, 2, 3, 'as', 4, 2, 12, 3, 1, 1, 1]
2
[1, 2, 3, 4, 2, 12, 3, 1, 1]
[1, 2, 4, 2, 12, 3, 1, 1, 2]
[2, 1, 1, 3, 12, 2, 4, 3, 2, 1]
[1, 1, 1, 2, 2, 2, 3, 3, 4, 12]
[1, 1, 1, 2, 2, 2, 3, 3, 4, 12]
1
-1
Press any key to continue . . .

2.4元组:不可变序列

print 1,2,3
print (1,2,3) #圆括号括起来
print ()      #空元组
print (2,)    #生成只有一个元素的元组
print 2*(2+3) #算式
print 2*(2+3,)#元组

#tuple序列转化为元组
print tuple([1,2,3])
print tuple("as")

输出结果

1 2 3
(1, 2, 3)
()
(2,)
10
(5, 5)
(1, 2, 3)
('a', 's')
Press any key to continue . . .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值