Python----------列表操作

1、列表的创建与删除

lst=["hello","world",98]    #使用中括号方式创建
print(lst)
lst2=list(["hello","world",98])      #使用内置对象创建
print(lst2)

   结果:

['hello', 'world', 98]
['hello', 'world', 98]

2、列表的查询操作

      获取列表中指定元素的索引

lst=["hello","world",98,"hello"]    #如果列表中存在相同元素,只返回列表中相同元素的第一个元素的索引
print(lst.index("hello"))
print(lst.index("hello",1,4))       #可以在指定的stat和stop之间查找
0
3

      获取列表中单个元素

lst=["hello","world",98,"hello","world",234]
print(lst[2])        #通过正向索引查询
print(lst[-3])       #通过负向索引查询
98
hello

      获取列表中多个元素

#--------------step步长为正数的情况-------------
lst=[10,20,30,40,50,60,70,80]
lst2=lst[1:6:1]
print(lst2)      #步长为1,将指定区间的列表元素拷贝到一个新的列表对象里

lst3=lst[1:6:2]  #步长为2
print(lst3)

lst4=lst[1:6]
print(lst4)      #没有指定步长时,则默认步长为1

lst5=lst[:6:2]   #没有指定start时,则默认start为1
print(lst5)

lst6=lst[1::2]   #没有指定stop时,则默认截至范围包括最后一个元素
print(lst6)

#---------------step步长为负数的情况------------
lst7=lst[::-1]
print(lst7)      #start和stop采用默认,起始元素为原列表中的最后一个元素,终止元素为原列表中第一个元素

lst8=lst[6:0:-2]
print(lst8)      
[20, 30, 40, 50, 60]
[20, 40, 60]
[20, 30, 40, 50, 60]
[10, 30, 50]
[20, 40, 60, 80]
[80, 70, 60, 50, 40, 30, 20, 10]
[70, 50, 30]

   判断指定元素是否在列表中存在

lst=[10,20,'python','hello']
print(10 in lst)          #判断指定元素是否在列表中
print(100 in lst)

for item in lst:          #依次遍历列表中的元素
    print(item)
True
False
10
20
python

3、列表元素的增删改操作

      列表元素的增加操作

      

#向例表的末尾添加一个元素
lst=[10,20,30]
print("添加元素之前",lst)
lst.append(100)
print("添加元素之后",lst)

#向列表的末尾添加多个元素
lst2=['hello','world']
""" lst.append(lst2) """     #将lst2作为一个元素添加到列表的末尾
lst.extend(lst2)             #一次性向列表的末尾添加多个元素
print(lst)

#在任意位置上添加一个元素
lst.insert(1,90)
print(lst)                   #在索引为1的位置上添加一个元素

#在列表的任意位置上添加N多个元素
lst3=[True,False,'hello']
lst[1:]=lst3                 #将索引为1的元素之后的元素切掉,替换新的多个元素
print(lst)

 

添加元素之前 [10, 20, 30]
添加元素之后 [10, 20, 30, 100]
[10, 20, 30, 100, 'hello', 'world']
[10, 90, 20, 30, 100, 'hello', 'world']
[10, True, False, 'hello']

 

列表元素的删除操作

                                        

#从列表中移除一个元素,如果有重复元素只移除重复元素的第一个
lst=[10,20,30,40,50,60,30]
lst.remove(30)
print(lst)

#pop()根据根据索引移除元素
lst.pop(1)
print(lst)
lst.pop()        #如果不指定索引参数,则删除列表中最后一个元素
print(lst)

print('----------------切片操作:删除至少一个元素,将产生一个新的列表------------------')
new_list=lst[1:3]
print("原列表",lst)
print("切片后的列表",new_list)

"""不产生新的列表对象,而是删除原列表中的内容"""
lst[1:3]=[]
print(lst)

"""清除列表中的所有对象"""
lst.clear()
print(lst)
[10, 20, 40, 50, 60, 30]
[10, 40, 50, 60, 30]
[10, 40, 50, 60]
----------------切片操作:删除至少一个元素,将产生一个新的列表------------------
原列表 [10, 40, 50, 60]
切片后的列表 [40, 50]
[10, 60]
[]

   列表元素的修改操作

lst=[10,20,30,40]
#一次修改一个值
lst[2]=100
print(lst)
#使用切片修改多个元素
lst[1:3]=[300,400,500,600]
print(lst)
[10, 20, 100, 40]
[10, 300, 400, 500, 600, 40]

4、列表元素的排序

lst=[20,29,39,98,54]
print("排序前的列表",lst)
lst.sort()                 #sort()方法,升序排序,此方法不会产生新的列表,只是在原表的基础上修改
print("排序后的列表",lst)

lst.sort(reverse=True)     #reverse=True 表示降序排序
print(lst)
lst.sort(reverse=False)
print(lst)                 #reverse=Faulse 表示升序排列

print('--------使用内置函数sorted()对列表进行排序,将产生一个新的列表对象')
lst=[20,29,39,98,54]
print("排序前的列表",lst)
new_lst=sorted(lst)
print("排序后的列表",new_lst)
#使用关键字参数,实现列表元素的降序排序
desc_list=sorted(lst,reverse=True)
print(desc_list)
排序前的列表 [20, 29, 39, 98, 54]
排序后的列表 [20, 29, 39, 54, 98]
[98, 54, 39, 29, 20]
[20, 29, 39, 54, 98]
--------使用内置函数sorted()对列表进行排序,将产生一个新的列表对象
排序前的列表 [20, 29, 39, 98, 54]
排序后的列表 [20, 29, 39, 54, 98]
[98, 54, 39, 29, 20]

5、列表生成式

lst=[i for  i in range(1,10)]
print(lst)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值