一、上节课知识回顾:

1.find通过元素找索引,可切片,找不到返回-1;

2.index,,找不到报错;

3.split 由字符串分割成列表,默认按空格分割;

4.captalize 首字母大写,其他字母小写;

5 upper 全大写;

6 lower 全小写;

7.title,每个单词首字母大写;

8.startswith 判断以什么为开头,可以切片,整体概念;

9.endswith 判断以什么为结尾,可以切片,整体概念;

10.format

"{} {} {}".format(有顺序)

"{0} {1} {2} {0}".format (有顺序的)

"{name} {age}".format(name=liu,age=10)

11.strip 默认去掉两侧空格(也可以指定去掉两端的某个元素)

12,lstrip,默认去掉左端的空格

 13.  rstrip 默认去掉右端的空格

14.center 居中,默认空格

15.count查找元素个数,可以切片,若元素为空则返回0

16.expandtabs 补全tab(\t)之前的空格,不足八个则补全八个,超过八个则补全为八的倍数

17.replace(old,new,次数)

18.isdigit,isalpha,isalnum(判断是否为数字,为字母,数字或者字母)

19.swapcase 大小写翻转

20.for i in 可迭代对象。

 二、今日内容:

一、列表的索引和切片

li=[1,2,3,"af","re","4"]
print(li[0])                     #1
print(li[4])                    #re
#切片
print(li[:3])                    #[1, 2, 3]
print(li[2:9])                  #[3, 'af', 're', '4']
#切片+步长
print(li[1:6:2])               #[2, 'af', '4']

二、列表的增,删,改,查

1、增加

(1)append 末尾增加(添加列表是作为一个整体添加)

li=[1,2,3,"af","re","4"]
print(li.append("dias"))       #None
print(li)                                # [1, 2, 3, 'af', 're', '4', 'dias']           

例子:输入员工姓名

name=[]
while  True:
    name1=input("请输入员工姓名:")
    if name1=="结束":break
    else:
        name.append(name1)
print(name)

(2)insert 按照指定位置增加元素

name=[12,"hasd","jfhu",123]
name.insert(2,"shfu")
print(name)                         #[12, 'hasd', 'shfu', 'jfhu', 123]

(3)extend  可迭代添加(添加列表是分解成元素添加进去)

name=[12,"hasd","jfhu",123]
name.extend([1,2,3])
print(name)                        #[12, 'hasd', 'jfhu', 123, 1, 2, 3]

2、删

(1)pop  默认删除是删除最后一个,按照索引的方式删除,有返回值

name=[12,"hasd","jfhu",123]
name1=name.pop(2)
print(name1)                     #jfhu
print(name)                      #[12, 'hasd', 123]

(2)remove  按照元素删除 无返回值

name=[12,"hasd","jfhu",123]
name.remove(12)
print(name)                     #['hasd', 'jfhu', 123]

(3)clear  清空列表

name=[12,"hasd","jfhu",123]
name.clear()
print(name)                 #[]

(4)del   以切片的方式删除 可以增加步长

name=[12,"hasd","jfhu",123,"frsdf","rurk"]
del name[0:5:2]
print(name)                #['hasd', 123, 'rurk']

3、改   切片先删除,迭代添加

三种情况

name=[12,"hasd","jfhu",123,"frsdf","rurk"]
# name[0]="男神"
# print(name)           #[“男”,“神”,'hasd', 123, 'rurk']
# name[0:3]="爱中hua"     #['爱', '中', 'h', 'u', 'a', 123, 'frsdf', 'rurk']
# print(name)
# name[0:3]=["dj","jasid"]
# print(name)                #['dj', 'jasid', 123, 'frsdf', 'rurk']

4、查

切片和for循环

name=[12,"hasd","jfhu",123,"frsdf","rurk"]
# print(name[2:4])     #['jfhu', 123]
for i in name:
    print(i)

三、列表的嵌套

查找
name=[12,"hasd","jfhu",[123,"frsdf"],"rurk"] # print(name[2]) #jfhu # print(name[2][1]) #f # print(name[1].capitalize()) #Hasd替换 # name[2]="adji" # print(name) #[12, 'hasd', 'adji', [123, 'frsdf'], 'rurk'] # name1=name[2].replace("jf","si") # print(name1) #sihu # print(name) #[12, 'hasd', 'jfhu', [123, 'frsdf'], 'rurk'] # name[3][1]="djiu" # print(name) #[12, 'hasd', 'jfhu', [123, 'djiu'], 'rurk']

四、sort 列表的正序排序

sort(reverse=True)列表的倒序排序

reverse l列表的翻转

number=[2,3,5,6,1,4,6,8,9]
number.sort()
print(number)           #[1, 2, 3, 4, 5, 6, 6, 8, 9]
number.sort(reverse=True)
print(number)            #[9, 8, 6, 6, 5, 4, 3, 2, 1]
number.reverse()
print(number)            #[1, 2, 3, 4, 5, 6, 6, 8, 9]

五、元组可切片但是不能更改,元组里面的列表内容可改

s=("snud","jsfidhj","hfuh",["dh","jdi","jifd"],"jfufh")
s[3][2]="wang"
print(s)                          
#('snud', 'jsfidhj', 'hfuh', ['dh', 'jdi', 'wang'], 'jfufh')

六、列表的嵌套循环

for i in range(1,10):
    print(i)
for i in range(1,10,2):
    print(i)
for i in range(10,1,-2):
    print(i)