chap6 (python全栈开发教程)

2022.11.4

1.列表的创建与删除 

# 1.列表的创建与删除
a=10  # 变量存储的是一个对象的引用
lst=['hello','world',98]
print(id(lst))
print(type(lst))
print(lst)

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

'''创建列表的第二种方式:使用内置函数list()'''
lst2=list(['hello','world',98])   # 0,1,2   -3,-2,-1

print(lst)
print(lst[0],lst[-1],lst[-3])
lst=['hello','world',98,'hello']
print(lst)

2.列表的查询操作 

# 2.列表的查询操作
'''获取列表中指定元素的索引'''
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 在’world’和98里找不到
print(lst.index('hello',1,4))  # 可以查找

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

'''获取列表中的多个元素'''
lst=[10,20,30,40,50,60,70,80]
# start=1,stop=6,step=1
print(lst[1:6:1])

print('原列表',id(lst))
lst2=lst[1:6:1]
print('切的片段:',id(lst2))
print(lst[1:6])  # 默认步长为1
print(lst[1:6:])  # 步长为1
# start=1,stop=6,step=2
print(lst[1:6:2])
# stop=6,step=2,start采用默认
print(lst[:6:2])
# start=1,step=2,stop采用默认
print(lst[1::2])

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

'''判断指定元素在列表中是否存在   in、not in'''
print('p' in 'python')     # True
print('k' not in 'python')  # True

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)

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

# 列表元素的增、删、改操作
'''1.列表元素的增加操作'''
'''①向列表的末尾添加一个元素'''
lst=[10,20,30]
print('添加元素之前',lst,id(lst))
lst.append(100)
print('添加元素之后',lst,id(lst))  # 没有创建新的列表对象,id相同,同一个列表对象

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

lst.extend(lst2)
print(lst)  # 把lst2的每一个元素都分别添加到列表的末尾 [10, 20, 30, 100, 'hello', 'world']

'''③在任意位置上添加一个元素'''
print(lst)  # [10, 20, 30, 100, 'hello', 'world']
lst.insert(1,90)  # 在索引为1的位置上添加90  [10, 90, 20, 30, 100, 'hello', 'world']
print(lst)

'''④在任意位置上添加n多个元素   (切片)'''
lst3=[True,False,'hello']
print(lst)   # [10, 90, 20, 30, 100, 'hello', 'world']
lst[1:]=lst3  # 切掉 [1:]
print(lst)   # [10, True, False, 'hello']

'''2.列表元素的删除操作'''
'''①remove()'''
lst=[10,20,30,40,50,60,30]
lst.remove(30)  # 从列表中一移除一个元素,如果有重复元素,之移除第一个
print(lst)    # [10, 20, 40, 50, 60, 30]
# lst.remove(100)  # ValueError: list.remove(x): x not in list

'''②pop() 根据索引移除元素'''
print(lst)    # [10, 20, 40, 50, 60, 30]
lst.pop(1)
print(lst)    # [10, 40, 50, 60, 30]
# lst.pop(5)    # 如果指定的索引位置不存在,将抛出异常 IndexError: pop index out of range
lst.pop()     # 如果不指定参数(索引),将删除最后一个元素
print(lst)    # [10, 40, 50, 60]

'''③切片操作-删除至少一个元素,将产生一个新的列表对象'''
new_list=lst[1:3]  # 想要切走1.2位置的元素
print('原列表',lst)   # [10, 40, 50, 60]
print('切片后的列表',new_list)  # [40, 50]
'''不产生新的列表对象,而是删除原列表中的内容'''
print('原列表',lst)  # [10, 40, 50, 60]
lst[1:3]=[]   # 用空列表替代位置
print(lst)  # [10, 60]

'''④清除列表中的所有元素'''
print(lst)  # [10, 60]
lst.clear()
print(lst)   # []

'''⑤del语句将列表对象删除'''
del lst
# print(lst)  # NameError: name 'lst' is not defined

'''3.列表元素的修改操作'''
'''①一次修改一个值'''
lst=[10,20,30,40]
lst[2]=100
print(lst)    # [10, 20, 100, 40]
'''②修改列表中的多个值'''
lst[1:3]=[300,400,500,600]
print(lst)   # [10, 300, 400, 500, 600, 40]

 4.列表元素的排序操作

# 列表元素的排序操作

'''1.sort()'''
lst=[20,40,10,98,54]
print('排序前的列表',lst,id(lst))
# 开始排序,调用列表对象的sort方法,默认升序排序
lst.sort()
print('排序后的列表',lst,id(lst))  # 标识没有发生更改,排序是在原列表的基础上进行的
# 通过指定关键字参数,将列表中的元素进行降序排序
lst.sort(reverse=True)
print(lst)   # [98, 54, 40, 20, 10]
lst.sort(reverse=False)  # 升序
print(lst)  # [10, 20, 40, 54, 98]

'''2.使用内置函数sorted()对列表进行排序,将产生一个新的列表对象'''
lst=[20,40,10,98,54]
print('原列表',lst,id(lst))
# 开始排序
new_list=sorted(lst)
print(lst,id(lst))
print(new_list,id(new_list))   # [10, 20, 40, 54, 98]
# 指定关键字参数,实现猎列表元素的降序排序
desc_list=sorted(lst,reverse=True)
print(desc_list,id(desc_list))

5.列表生成式

# 列表生成式

lst=[i for i in range(1,10)]  # for i in range(1,10):1到9整数序列;[]:放到列表中;i:产生的整数序列是i,列表中存储的是i的值
print(lst)  # [1, 2, 3, 4, 5, 6, 7, 8, 9]

lst=[i*i for i in range(1,10)]  # 列表中最终存储的是前面这个值:i*i--表示列表元素的表达式
print(lst)   # [1, 4, 9, 16, 25, 36, 49, 64, 81]

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值