列表[] 索引与切片
#例题
#li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# #例子1 找出列表中索引为0的元素
# l1 = li[0]
# print(l1)
# 结果输出:
# yijiajun
# #例子2 找出列表中索引为1的元素
# l2 = li[1]
# print(l2)
# 结果输出:
# [1, 3, 5, 7, 9]
#例子3 (对列表切片,要遵循顾头不顾尾的原则)对首为索引0,尾为索引3的列表切片。
# l3 = li[0:3]
# print(l3)
# 结果输出:
# ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang']
列表增加用法
#第一种方法:列表增加 append() 格式: append(value)元素默认在末尾增加
#PS:低级错误:append()只是一个新增动作,如果把li.append('王强')打印出来,会输出None.
#例题
# li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# l3 = li.append('王强')
# print("append()的低级错误---->print(li.append('王强')) ",li.append('王强'))
# print(li)
# #输出结果:
# # append()的低级错误---->print(li.append('王强')) None
# # ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong', '王强', '王强']
#第二种方法:插入 insert() 格式: insert(index,value)
#PS:低级错误:insert()只是一个新增动作,如果把li.insert(2.2)打印出来,会输出None.
# li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# l4 = li.insert(2,2) #表示在列表索引2的位置插入数字2
# print("insert()的低级错误----print(li.insert(2.2)) ",li.insert(2,2))
# print(li)
# 输出结果:
# insert()的低级错误----print(li.insert(2.2)) None
# ['yijiajun', [1, 3, 5, 7, 9], 2, 2, 'zhangliang', 'zhaoritian', 'sunwukong']
#第三种方法:extend() 迭代增加 自动把字符串拆分为最小元素
#PS:低级错误:extend()只是一个新增动作,如果打印出来会报None
#例题
# li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# l5 = li.extend('Iloveyou')
# print(li)
# 输出结果:
# ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong', 'I', 'l', 'o', 'v', 'e', 'y', 'o', 'u']
列表删除用法:
#第一种方法:pop() 按索引删除,如果为空,默认删除最后一个元素。(打印会有返回值)
# li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# l6 = li.pop(2) #删除列表中索引为2的元素
# #l7 = li.pop() #默认删除列表中最后一个元素
# print(li.pop(2),li)
# print(li.pop())
# 输出结果:
# zhaoritian ['yijiajun', [1, 3, 5, 7, 9], 'sunwukong']
# sunwukong
#第二种方法:remove() 按元素value删除
#PS:remove() 打印输出None
# li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# l8 = li.remove('yijiajun')
# print(l8,li)
# 输出结果:
# None [[1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
#第三种方法:del li 默认删除列表,可以切片
#PS:del是python语句,而不是列表方法,无法通过list来调用。使用del可以删除一个元素,当元素删除之后,位于它后面的元素会自动移动填补空出来的位置。
# li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# del li[0:2]
# print(li)
# 输出结果:
# ['zhangliang', 'zhaoritian', 'sunwukong']
#第四种方法:列表清空 clear()
#PS:打印clear()会输出None
# li = ['yijiajun',[1,3,5,7,9],'zhangliang','zhaoritian','sunwukong']
# l9 = li.clear()
# print(li)
# 输出结果;
# []
列表:改
#第一种方法:赋值(通过index索引指定位置修改)
# li = ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
# li[0] = 'zhangshaohan'
# print(li)
# 输出结果:
# ['zhangshaohan', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
#第二种方法:切片
# li = ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
# #li[0:3] = 'love' #切片修改会把字符串拆分为最小元素
# li[0:3] = [2,4,5,6]
# print(li)
# 输出结果:
# [2, 4, 5, 6, 'zhaoritian', 'sunwukong']
#列表查询
#第一种方法:for有限循环
# li = ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
# for i in li:
# print(i)
# 输出结果:
# yijiajun
# [1, 3, 5, 7, 9]
# zhangliang
# zhaoritian
# sunwukong
#第二种方法: print()
# li = ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
# print(li[3])
# print(li[0:3])
# 输出结果:
# zhaoritian
# ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang']
# ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
公共方法:len() count() index() ps:在列表中不支持find()处理
# li = ['yijiajun', [1, 3, 5, 7, 9], 'zhangliang', 'zhaoritian', 'sunwukong']
# print('测量长度',len(li))
# print('统计字符yijiajun出现的次数',li.count('yijiajun'))
# print('查找字符zhangliang的索引',li.index('zhangliang'))
# 输出结果:
# 测量长度 5
# 统计字符yijiajun出现的次数 1
# 查找字符zhangliang的索引 2
排序
#正向排序 sort()
#PS:打印sort(),会输出返回None
li = [1,4,5,2,9,3]
# print(li.sort(),li)
#输出结果:
#None [1, 2, 3, 4, 5, 9]
#反向排序 sort(reverse=True)
#PS:打印sort(),会输出返回None
# print(li.sort(reverse=True),li)
# 输出结果:
# None [9, 5, 4, 3, 2, 1]
#反转:reverse()
#PS:打印reverse() ,会输出返回None
# print(li.reverse(),li)
# 输出结果:
# None [1, 2, 3, 4, 5, 9]
列表嵌套
# li = ['yijiajun','zoukai','caoyuetent','hulian',['lizhen','yanggou',100],'caihanyu']
# print(li[0][0])
# print('大小写互换',li[0].swapcase())
# print('首字母大写',li[0].capitalize())
# 输出结果:
# y
# YIJIAJUN
# Yijiajun
#替换
# l11 = li[0].replace('jiajun','jiaping')
# print(l11)
# 输出结果:
# yijiaping
#课间作业
#把列表中100的元素,变成99(用两种方法)
#方法一
# li[4][2] = 89
# print(li)
#方法二
# li[4][2] = str(li[4][2])
# d1 = int(li[4][2].replace('100','89'))
# li[4][2] = int(li[4][2])
# print(li)
# 输出结果:
# ['yijiajun', 'zoukai', 'caoyuetent', 'hulian', ['lizhen', 'yanggou', 100], 'caihanyu']
元祖(只读,可循环查询,可切片)
#PS:元祖本身不能修改只可读,但是如果元祖里嵌套了列表就可能可以修改
# yu = (1,3,5,'yijiajun',[2,4,6],'zoukai')
# print(yu[3])
# print(yu[0:4])
# print(yu[::-1])
# print(yu[-1::-1])
# print(yu[-1::-2])
# 输出结果:
# yijiajun
# (1, 3, 5, 'yijiajun')
# ('zoukai', [2, 4, 6], 'yijiajun', 5, 3, 1)
# ('zoukai', [2, 4, 6], 'yijiajun', 5, 3, 1)
# ('zoukai', 'yijiajun', 3)
#for有限循环查询
# for i in yu:
# print(i)
# 输出结果:
# 1
# 3
# 5
# yijiajun
# [2, 4, 6]
# zoukai
#常用方法操作
# print(yu[3].upper())
# print(yu[3][0].upper())
# print(yu[-1].capitalize())
# 输出结果:
# YIJIAJUN
# Y
# Zoukai
#验证元祖是否是本身不能修改只可读,但是如果元祖里嵌套了列表就可能可以修改
# a1 = yu.append('zhang')
# print(li)
# 输出结果:
# a1 = yu.append('zhang')
# AttributeError: 'tuple' object has no attribute 'append'
# tu = yu[4].append('zhangxia')
# print(yu)
# 输出结果:
# (1, 3, 5, 'yijiajun', [2, 4, 6, 'zhangxia'], 'zoukai')
#join() 用于将序列中的元素以指定的字符连接生成一个新的字符串。
#PS:join最后返回字符串 转换:列表转换字符串用join(),字符串转列表用split()
# 例子
# li = ['1','2','3','beauty']
# l0 = ','.join(li)
# print(l0.split(','),l0,type(l0))
# range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。list() 函数是对象迭代器,可以把range()返回的可迭代对象转为一个列表,返回的变量类型为列表。
#PS:顾头不顾尾,可加步长,如果是从小到大生成迭代对象,加步长,步长不能为负,否则会不运行返回空;如果要从大到小生成迭代对象,加步长,步长不能为正,否则会不运行返回空(面试套路)。
# for i in range(0,5):
# print(i)
# #相等于
# for i in range(5):
# print(i)
#例1
# ran = range(0,10,1)
# for i in ran:
# print(i)
# 结果输出:
# 0
# 至
# 9
#例2
# ran = range(0,10,-1)
# for i in ran:
# print(i)
# 结果输出
# 空
#例3
# ran = range(10,0,-1)
# for i in ran:
# print(i)
# 结果输出:
# 10
# 至
# 1
# #例4
# ran = range(10,0,1)
# for i in ran:
# print(i)
# 结果输出:
# 空
# 课间作业:把tu元祖里的数据依次打印出来(包含嵌套列表中的数据)
tu = ('a','c','d',[1,2,3,4,'g'],1,2,3,'bb')
#方法1
for i in tu:
if type(i) == list:
for k in i:
print(k)
else:
print(i)
#方法二
for i in range(len(tu)):
if type(tu[i]) == list:
for k in tu[i]:
print(tu[i][k])
else:print(tu[i])
#方法三
tu = ('a','c','d',[1,2,3,4,'g'],1,2,3,'bb')
index = 0
k = 0
while index < len(tu):
if type(tu[index]) == list:
while k < len(tu[index]):
print(tu[index][k])
k += 1
else:
index += 1
else:
print(tu[index])
index += 1