Python 学习笔记2[列表]

1.列表

序列都可以进行的操作包括索引,切片,加,乘,检查成员,Python已经内置确定序列的长度以及确定最大最小的元素的方法。
列表的数据项不需要具有相同的类型

1.1 创建列表

创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。列表索引从0开始。如下所示:

list1 = ['Google',  1997, 2000[1,2,3]];

1.2 列表操作

增加

添加单个元素append:list.append(obj) 在列表末尾添加新的对象。obj – 添加到列表末尾的对象。
该方法无返回值,但是会修改原来的列表。

>>> list1=[1,2,'弎']
>>> list1.append(4)
>>> list1
[1, 2, '弎', 4]
>>> type(list1)
<class 'list'>
>>> 

添加多个元素extend:list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)。seq – 列表。
该方法没有返回值,但会在已存在的列表中添加新的列表内容。

>>> list2=[5,'五']
>>> list1.extend(list2)
>>> list1
[1, 2, '弎', 4, 5, '五']
-----------------------
>>> list1=[1, 2, '弎', 4, 5, '五']
>>> list1.extend([6,6,6])
>>> list1
[1, 2, '弎', 4, 5, '五', 6, 6, 6]

append 与extend 的区别:

>>> list1=[1, 2, '弎', 4,]
>>> list1
[1, 2, '弎', 4]
>>> list2=[5,'五']
>>> list1.append(list2)
>>> list1
[1, 2, '弎', 4, [5, '五']]

插入元素insert:list.insert(index,obj) 用于将指定对象插入列表的指定位置。
index – 对象 obj 需要插入的索引位置。
obj – 要插入列表中的对象。
该方法没有返回值,但会在列表指定位置插入对象

>>> list1.insert(4,'wuwuwu')
>>> list1
[1, 2, '弎', 4, 'wuwuwu', 5, '五']

拼接:+

>>> list1
[1, 2, '弎', 4, 'wuwuwu', 5, '五']
>>> list1
[1, 2, '弎', 4, 'wuwuwu', 5, '五']
>>> list2
[5, '五']
>>> list1+list2
[1, 2, '弎', 4, 'wuwuwu', 5, '五', 5, '五']

复制:*

>>> list2
[5, '五']
>>> list2*4
[5, '五', 5, '五', 5, '五', 5, '五']
获取

获取单个元素[index]:通过索引值index 获取单个元素,index从0开始;

>>>list1=[1, 2, '弎', 4, 'wuwuwu', 5, '五']
>>> list1[3]
4

list[-index] 获取倒数第index个元素;

>>> list1=[1, 2, '弎', 4, 'wuwuwu', 5, '五']
>>> list1[-1]
'五'

元素互换:

>>> list1=[0,1,2,3,4,5]
>>> list1[3],list1[4]=list1[4],list1[3]
>>> list1
[0, 1, 2, 4, 3, 5]

获取多个元素-列表分片[slice]: list[began_index:end_index:step ] end_index 位置元素不包含;step 可省略,默认为1;
列表分片后获得原始列表的拷贝,原始列表不变;

>>> list1=[0,1,2,3,4,5,6,7,8]
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list1[2:5]
[2, 3, 4]
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8]

没有began_index 从0开始,没有end_index 到列表最后为止,只有: 拷贝整个列表;

>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list1[:5]
[0, 1, 2, 3, 4]
>>> list1[2:]
[2, 3, 4, 5, 6, 7, 8]
>>> list1[:]
[0, 1, 2, 3, 4, 5, 6, 7, 8]

step 为2:

>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list1[1:8:2]
[1, 3, 5, 7]

step为-1; 拷贝一个分片内元素反续的列表:

>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> list1[::-1]
[8, 7, 6, 5, 4, 3, 2, 1, 0]

复制:list.copy() == list[:]

>>> list1
[1, 2, 4, 4, 5]
>>> list2=list1.copy()
>>> list2
[1, 2, 4, 4, 5]
>>> list1[2]=3
>>> list1
[1, 2, 3, 4, 5]
>>> list2
[1, 2, 4, 4, 5]
删除

删除元素remove:list.remove(obj) 用于移除列表中某个值的第一个匹配项。
obj – 列表中要移除的对象。该方法没有返回值但是会移除列表中的某个值的第一个匹配项。

>>> list3=[1,2,'s',3,4,'s',5,6,7]
>>> list3.remove('s')
>>> list3
[1, 2, 3, 4, 's', 5, 6, 7]

删除指定位置元素pop: list.pop([index==-1]) 于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
index – 可选参数,要移除列表元素的索引值,不能超过列表总长度,默认为 index=-1,删除最后一个列表值。该方法返回从列表中移除的元素对象。

>>> list3
[1, 2, 3, 4, 's', 5, 6, 7]
>>> list3.pop()
7
>>> list3
[1, 2, 3, 4, 's', 5, 6]
>>> list3.pop(4)
's'
>>> list3
[1, 2, 3, 4, 5, 6]
>>> 

删除列表del: del list[index] 删除列表或者列表index位置处元素;del 是一个语句;

>>> list3
[1, 2, 3, 4, 5, 6]
>>> del list3[5]
>>> list3
[1, 2, 3, 4, 5]
>>> del list3
>>> list3
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    list3
NameError: name 'list3' is not defined

清空列表:list.clear() == del list[:]

>>> list1
[1, 2, 3, 4, 5]
>>> list1.clear()
>>> list1
[]
-------------------------
>>> list1=[1,2,3,4,5]
>>> del list1[:]
>>> list1
[]
其余操作

比较<,>: 从列表的第一个元素比较;
成员操作符in /not in :

>>> list4=[4,4,3,8,9]
>>> 1 in list4
False
>>> 4 in list4
True
>>> 1 not in list4
True

len(list) 方法返回列表元素个数。

>>> list4
[4, 4, 3, 8, 9]
>>> len(list4)
5

max/min(): 返回最大最小元素;

>>> list4
[4, 4, 3, 8, 9]
>>> len(list4)
5
>>> max(list4)
9
>>> min(list4)
3

统计元素出现个数:list.count(obj)

>>> list4
[4, 4, 3, 8, 9]
>>> list4.count(4)
2

查找出现位置:list.index(x[, start[, end]])
x-- 查找的对象。
start-- 可选,查找的起始位置。
end-- 可选,查找的结束位置。
该方法返回查找对象的索引位置,如果没有找到对象则抛出异常。

>>> list4
[4, 4, 3, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list4.index(8)
3
>>> list4.index(8,4)
12
>>> list4.index(8,4,10)
Traceback (most recent call last):
  File "<pyshell#44>", line 1, in <module>
    list4.index(8,4,10)
ValueError: 8 is not in list

列表排序:
list.reverse() 翻转列表:

>>> list5=[1,2,3,4,5]
>>> list5.reverse()
>>> list5
[5, 4, 3, 2, 1]

切片slice输出翻转拷贝:

>>> list5
[5, 4, 3, 2, 1]
>>> list5[::-1]
[1, 2, 3, 4, 5]

sort()排序:
list.sort(cmp=None, key=None, reverse=False) 于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
cmp – 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
key – 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
reverse – 排序规则,reverse = True 降序, reverse = False 升序(默认)。
该方法没有返回值,但是会对列表的对象进行排序。

# 获取列表的第二个元素
def takeSecond(elem):
    return elem[1]
# 列表
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# 指定第二个元素排序
random.sort(key=takeSecond)
# 输出类别
print ('排序列表:', random)

>>> 排序列表: [(4, 1), (2, 2), (1, 3), (3, 4)]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值