其他分享
首页 > 其他分享> > 列表note

列表note

作者:互联网

poem = ['杳','杳','寒','山','道']
print(poem[-2]) #列表索引负数
#切片
print(poem[1:])
print(poem[2:3:1])
print(poem[2:3])
print(poem[::-1]) #倒叙
print(poem[:])  #建立副本
#修改
print('修改前:',poem)
#1.直接通过索引
poem[0] = '啊'
poem[4] = 'he'
print('修改后:',poem)
#2.通过切片修改
print('切片修改前:',poem)
poem[0:2]=['好','和']   #使用新元素代替旧元素
poem[0:2]=['好','和','还'] #多的元素会插入列表
poem[0:0]=['哈哈']#给索引为0处插入元素

print('切片修改后:',poem)
#删除  del
poem1=['落','落','来','鉴','别']
del poem1[0:2] #删除索引位置的
print(poem1)
del poem1[::2] #删除步长为2
print(poem1)
#列表的遍历


#通过while循环遍历
lib=['luzi','hh','dou','xixi','huhu']
i=0
while i <len(lib):
    print(lib[i])
    i+=1

#通过for循环遍历
for s in lib:
    print(s)

 

标签:poem,索引,列表,note,修改,print,poem1,切片
来源: https://www.cnblogs.com/byhsd/p/11162983.html