python基础(二)之列表操作

为什么需要列表

在这里插入图片描述

a=10 #变量存储的是一个对象的引用
lst=['hello','world',98]
print(id(lst))
print(type(lst))
print(lst)
#输出结果
2652627143168
<class 'list'>
['hello', 'world', 98]

列表的创建

在这里插入图片描述

'''创建列表的第一种方式,使用[]'''
lst=['hello','world',98]

'''创建列表的第二种方式,使用内函数list()'''
lst2=list(['hello','world',98])

列表的特点

在这里插入图片描述

列表的查询操作

●获取列表中指定元素的索引
在这里插入图片描述

lst=['hello','world',98,'hello']
print(lst.index('hello'))      # 如果列表中有相同元素只返回列表中相同元素的第一个元素索引
#print(lst.index('python'))    # ValueError: 'python' is not in list
#print(lst.index('hello',1,3)) # ValueError: 'hello' is not in list
print(lst.index('hello',1,4))  #3

●获取列表中的单个元素
在这里插入图片描述

lst=['hello','hello','98','hello','world',234]
#获取索引为2的元素
print(lst[2])        #98
#获取索引为-3的元素
print(lst[-3])       #hello
#获取索引为10的元素
print(lst[10])       #IndexError: list index out of range

●获取列表的多个元素
在这里插入图片描述

lst=[10,20,30,40,50,60,70,80]
print('原列表',id(lst))
lst2=lst[1:6:1]
print(lst2)
print('切的片段:',id(lst2))
print(lst[1:6:2])
#输出结果
原列表 2648804755968
[20, 30, 40, 50, 60]
切的片段: 2648804753920
[20, 40, 60]

print('------------------step步长为负数的情况------------------------')
print('原列表:',lst)
print(lst[::-1])
#start=7,stop 省略 step=-1
print(lst[7::-1])
#start=6,stop=0,step=-2
print(lst[6:0:-2])
#输出结果
原列表: [10, 20, 30, 40, 50, 60, 70, 80]
[80, 70, 60, 50, 40, 30, 20, 10]
[80, 70, 60, 50, 40, 30, 20, 10]
[70, 50, 30]

●判断指定元素在列表中是否存在
在这里插入图片描述

lst=[10,20,'python','hello']
print(10 in lst)
print(100 in lst)
print(10 not in lst)
print(100 not in lst)
for item in lst:
    print(item)
#输出结果
True
False
False
True
10
20
python
hello

●列表元素的增加操作
在这里插入图片描述

#向列表的末尾添加一个元素
lst=[10,20,30]
print('添加元素之前',lst,id(lst))
lst.append(100)
print('添加元素之后',lst,id(lst))
lst2=['hello','world']
lst.append(lst2) #将lst2作为一个元素添加到列表的末尾
print(lst)
#输出结果
添加元素之前 [10, 20, 30] 2111305550336
添加元素之后 [10, 20, 30, 100] 2111305550336
[10, 20, 30, 100, ['hello', 'world']]
#######################################################################################
#lst.append(lst2) #将lst2作为一个元素添加到列表的末尾
lst.extend(lst2) #向列表的末尾一次性添加多个元素
print(lst)
[10, 20, 30, 100, 'hello', 'world']
########################################################################################
#在任意位置上添加一个元素
lst.insert(1,90)
print(lst)
########################################################################################
#在任意的位置上添加N多个元素(切片)
print('切片之前',lst)
lst3=['a','b','c']
lst[1:]=lst3
print('切片之后',lst)
切片之前 [10, 90, 20, 30, 100, 'hello', 'world']
切片之后 [10, 'a', 'b', 'c']

●列表元素的删除
在这里插入图片描述

lst=[10,20,30,40,50,60,30]
lst.remove(30)  #从列表中移除第一个元素,如果有重复元素只移除第一个元素
print(lst)
lst.remove(100) #ValueError: list.remove(x): x not in list
[10, 20, 40, 50, 60, 30]
lst=[10,20,30,40]
#pop()根据索引移除元素
print('移除之前列表为',lst)
lst.pop(1)
print(lst)
#lst.pop(5) #IndexError: pop index out of range
lst.pop() #如果不指定参数(索引),将删除列表中的最后一个元素
print(lst)
移除之前列表为 [10, 20, 30, 40]
[10, 30, 40]
[10, 30]
#########################################################################################
print('-------切片操作删除至少一个元素,将产生一个新的列表对象-----------')
lst=[10,20,30,40,50]
new_lst=lst[1:3]
print('原列表',lst)
print('切片后的列表',new_lst)

'''不产生新的列表对象,而是删除原列表中的内容'''
lst[1:3]=[]
print(lst)
#输出结果
原列表 [10, 20, 30, 40, 50]
切片后的列表 [20, 30]
[10, 40, 50]
#########################################################################################
'''清除列表中的所有元素'''
lst.clear()
print(lst)
#输出结果
[]
#########################################################################################
'''del语句将列表对象删除'''
del lst
print(lst) 
#输出结果
NameError: name 'lst' is not defined

●列表元素的修改操作
在这里插入图片描述

#一次修改一个值
lst[2]=100
print(lst)
#基于索引修改多个值
lst[1:3]=[300,400,500,600]
print(lst)
#输出结果
[10, 20, 30, 40]
[10, 20, 100, 40]
[10, 300, 400, 500, 600, 40]

●列表元素的排序操作
在这里插入图片描述

lst=[20,40,10,98,54]
print('排序前的列表',lst,id(lst))
lst.sort()
print('排序后的列表',lst,id(lst))
排序前的列表 [20, 40, 10, 98, 54] 1755432684160
排序后的列表 [10, 20, 40, 54, 98] 1755432684160
#######################################################################################
lst.sort(reverse=True)
print(lst)
lst.sort(reverse=False)
print(lst)
[98, 54, 40, 20, 10]
[10, 20, 40, 54, 98]
#######################################################################################
lst=[20,40,10,98,54]
print('排序前的列表',lst)
new_lst=sorted(lst)
print(new_lst)
desc_lst=sorted(lst,reverse=True)
print(desc_lst)
排序前的列表 [20, 40, 10, 98, 54]
[10, 20, 40, 54, 98]
[98, 54, 40, 20, 10]

●列表生成式
在这里插入图片描述

'''列表中的元素的值为2,4,6,8,10'''
lst2=[i*2 for i in range(1,6)]
print(lst2)

总结:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值