python基础9 列表,元组操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础9 列表,元组操作相关的知识,希望对你有一定的参考价值。
本节内容:
1 列表
2 元组
3 参考网页
列表
Python内置的一种数据类型是列表:list。list是一种有序的集合,可以随时添加和删除其中的元素。
列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作。
定义列表
names = [‘Michael‘, ‘Bob‘, ‘Tracy‘]
访问列表
通过索引访问
通过下标访问列表中的元素,下标从0开始计数。 使用索引时,千万注意不要越界。
>>> names[0]
‘Michael‘
>>> names[2]
‘Tracy‘
倒序访问
千万注意不要越界。
>>> names[-1] ] #倒着取,倒数第一个
‘Tracy‘
>>> names[-2] #倒着取,倒数第二个
‘Bob‘
切片:访问多个元素
name = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names[1:4] #取下标1至下标5之间的数字,包括1,不包括4
[‘jack‘, ‘jerry‘, ‘lucy‘,‘lily‘]
names[1:-1] #取下标1至-1的值,不包括-1
[‘jack‘,‘jerry‘,‘lucy‘,‘lily‘,‘carry‘]
names[0:3]
[‘cathy‘,‘jack‘,‘jerry‘]
names[:3] #如果是从头开始取,0可以忽略,跟上句效果一样
[‘cathy‘,‘jack‘,‘jerry‘]
names[3:] #如果想取最后一个,必须不能写-1,只能这么写
[‘lucy‘,‘lily‘,‘carry‘,‘abby‘]
names[3:-1] #这样-1就不会被包含了
[‘lucy‘,‘lily‘,‘carry‘]
names[0::2] #后面的2是代表,每隔一个元素,就取一个
[‘cathy‘, ‘jerry‘, ‘lily‘,‘abby‘]
names[::2] #和上句效果一样
[‘cathy‘, ‘jerry‘, ‘lily‘,‘abby‘]
追加
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names.append("我是新来的")
print(names)
结果:
[‘cathy‘, ‘jack‘, ‘jerry‘, ‘lucy‘, ‘lily‘, ‘carry‘, ‘abby‘, ‘我是新来的‘]
插入
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names.insert(2,"强行从2索引位置插入数据")
print(names)
结果:
[‘cathy‘, ‘jack‘, ‘强行从2索引位置插入数据‘, ‘jerry‘, ‘lucy‘, ‘lily‘, ‘carry‘, ‘abby‘]
修改
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names[2] = "索引为2的位置修改"
print(names)
结果:
[‘cathy‘, ‘jack‘, ‘索引为2的位置修改‘, ‘lucy‘, ‘lily‘, ‘carry‘, ‘abby‘]
删除
- 例1 del
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
del names[2] #delete索引为2的元素
print(names)
结果:
[‘cathy‘, ‘jack‘, ‘lucy‘, ‘lily‘, ‘carry‘, ‘abby‘]
- 例2 remove()
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names.remove("jack") #删除指定元素
print(names)
结果:
[‘cathy‘, ‘jerry‘, ‘lucy‘, ‘lily‘, ‘carry‘, ‘abby‘]
- 例3 pop()
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names.pop() #删除列表最后一个值"abby"
print(names)
结果:
[‘cathy‘, ‘jack‘, ‘jerry‘, ‘lucy‘, ‘lily‘, ‘carry‘]
pop()方法总是删掉list的最后一个元素
#删除jerry
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names.pop(2) #删除列表索引为2的值
print(names)
结果:
[‘cathy‘, ‘jack‘, ‘lucy‘, ‘lily‘, ‘carry‘, ‘abby‘]
names.pop(3) IndexError: pop index out of range
扩张
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
b = [1,2,3]
names.extend(b)
print(names)
结果:
[‘cathy‘, ‘jack‘, ‘jerry‘, ‘lucy‘, ‘lily‘, ‘carry‘, ‘abby‘, 1, 2, 3]
统计
names = ["cathy","lily","jerry","lucy","lily","carry","abby"]
cnum = names.count("lily")
print(cnum)
结果: 2
排序
- 例1
names = ["cathy","jack","jerry","lucy","lily","carry","abby"]
names.sort() #排序
print(names)
结果:
[‘abby‘, ‘carry‘, ‘cathy‘, ‘jack‘, ‘jerry‘, ‘lily‘, ‘lucy‘]
- 例2
names = ["cathy","jack","jerry",1,2,3]
names.sort() #排序
print(names)
结果会报如下的错误:
names.sort() #排序
TypeError: unorderable types: int() < str()
- 例3
names = ["cathy","jack","jerry","1","2","3"]
names.sort()
print(names)
结果:
[‘1‘, ‘2‘, ‘3‘, ‘cathy‘, ‘jack‘, ‘jerry‘]
列表的排序按asii顺序排序。
反转
names = ["cathy","jack","jerry","1","2","3"]
names.reverse() #反转
print(names)
结果:
[‘3‘, ‘2‘, ‘1‘, ‘jerry‘, ‘jack‘, ‘cathy‘]
获取下标
names = ["cathy","lily","jerry","lucy","lily","carry","abby"]
nid = names.index("lily") #只返回找到的第一个下标
print(nid)
结果: 1
拷贝
Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的。
对象赋值
在python中,对象赋值实际上是对象的引用。当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用。 当对对象做赋值或者是参数传递或者作为返回值的时候,总是传递原始对象的引用,而不是一个副本。
list = ["cathy","jack","jerry",["bruce","85"]]
b=list
list.append(5)
list[3].append("75")
print(list)
print(b)
结果:
[‘cathy‘, ‘jack‘, ‘jerry‘, [‘bruce‘, ‘85‘, ‘75‘], 5]
[‘cathy‘, ‘jack‘, ‘jerry‘, [‘bruce‘, ‘85‘, ‘75‘], 5]
原始列表改变,被赋值的b也会做相同的改变。
浅拷贝
copy浅拷贝,没有拷贝子对象,所以原始数据改变,新copy的子对象会改变。
import copy
list = ["cathy","jack","jerry",["lily","100"]]
c=copy.copy(list)
print(list,c)
list.append(5)
print(list,c)
list[3].append(‘99‘)
print(list)
print(c)
结果:
[‘cathy‘, ‘jack‘, ‘jerry‘, [‘lily‘, ‘100‘]] [‘cathy‘, ‘jack‘, ‘jerry‘, [‘lily‘, ‘100‘]]
[‘cathy‘, ‘jack‘, ‘jerry‘, [‘lily‘, ‘100‘], 5] [‘cathy‘, ‘jack‘, ‘jerry‘, [‘lily‘, ‘100‘]]
[‘cathy‘, ‘jack‘, ‘jerry‘, [‘lily‘, ‘100‘, ‘99‘], 5]
[‘cathy‘, ‘jack‘, ‘jerry‘, [‘lily‘, ‘100‘, ‘99‘]] #里面的子对象被改变了
names = ["cathy","lily","jerry",["abby","95"]]
name_copy = names.copy()
names.append("bruce")
names[3].append("60")
print(name_copy)
print(names)
结果显示:
[‘cathy‘, ‘lily‘, ‘jerry‘, [‘abby‘, ‘95‘, ‘60‘]]
[‘cathy‘, ‘lily‘, ‘jerry‘, [‘abby‘, ‘95‘, ‘60‘], ‘bruce‘]
深拷贝
深拷贝,包含对象里面的自对象的拷贝,所以原始对象的改变不会造成深拷贝里任何子元素的改变。
import copy
names = ["cathy","lily","jerry",["abby","95"]]
d=copy.deepcopy(names)
print(names)
print(d)
names.append("jack")
names[3].append("70")
print(names)
print(d)
结果显示:
[‘cathy‘, ‘lily‘, ‘jerry‘, [‘abby‘, ‘95‘]]
[‘cathy‘, ‘lily‘, ‘jerry‘, [‘abby‘, ‘95‘]]
[‘cathy‘, ‘lily‘, ‘jerry‘, [‘abby‘, ‘95‘, ‘70‘], ‘jack‘]
[‘cathy‘, ‘lily‘, ‘jerry‘, [‘abby‘, ‘95‘]] #始终没有改变
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
它只有2个方法,一个是count,一个是index,完毕。
names = ("cathy","jack","lucy","cathy")
nid = names.index("cathy")
ncnum = names.count("cathy")
print(nid)
print(ncnum)
结果:
0
2
参考网页
http://www.cnblogs.com/alex3714/articles/5717620.html
http://www.imooc.com/code/3355
http://blog.csdn.net/lixintong1992/article/details/51005546
http://www.cnblogs.com/kellyseeme/p/5525067.html
http://www.cnblogs.com/xueli/p/4952063.html
http://python.jobbole.com/82294/
以上是关于python基础9 列表,元组操作的主要内容,如果未能解决你的问题,请参考以下文章