标签:end ever 倒数 大小写 lis 修改 调用 sort false
list = ["Hello","my","name","is","Python"]
list.append(".") #末尾追加
print(list)
list.insert(1,",") #根据下标插入内容
print(list)
list_extend = ["WOW","YOU","ARE","COOL!"]
list.extend(list_extend) #entend 方法,即将一个列表元素,追加到被调用列表中,括号内列表元素不做改变
print("list",list) #list [‘Hello‘, ‘,‘, ‘my‘, ‘name‘, ‘is‘, ‘Python‘, ‘.‘, ‘WOW‘, ‘YOU‘, ‘ARE‘, ‘COOL!‘]
print("list_extend",list_extend) #list_extend [‘WOW‘, ‘YOU‘, ‘ARE‘, ‘COOL!‘]
list = ["Hello","my","name","is","Python"]
list.remove("Hello") #删除,无返回值
print(list)
list.pop(1)#根据下标删除,返回被删除列表元素
print(list.pop(1))
list.clear() #清空列表元素
list_two = [1,2,3,4,5,6]
print(list_two[1:])#从第2个元素取到最后 [2, 3, 4, 5, 6]
print(list_two[1:-1])#取第2个元素和倒数第2个元素之间[2, 3, 4, 5] 顾头,所以1为2,不顾尾,所以是-1的前一个元素,即5
print(list_two[1??1])#[2, 3, 4, 5] 依然不取两边值,但[1??1]中,第3个1代表步长
print(list_two[1??2])#[2, 4] 步长为2,即隔一个取一个
list_three = [1,2,3,4,5,6]
list_three[0] = 0 #[0, 2, 3, 4, 5, 6] 直接通过下标修改,将1改为0
print(list_three)
list_three[0??2] = [0,0,0] #[0, 2, 0, 4, 0, 6] 通过切片进行修改
print(list_three)
list_four = [1,2,2,"A","a","a"]
print(list_four.count(2))#2 1出现2次
print(list_four.count("a"))#2 计数区分大小写 出现2次a
list_five = [2,4,1,3,5,7,6]
list_five.sort() #进行升序排列 默认reverse=False 可以不写
print(list_five) #[1, 2, 3, 4, 5, 6, 7]
list_five.sort(reverse=True) #sort 传入reverse参数为真是,排序为倒叙
print(list_five) #[7, 6, 5, 4, 3, 2, 1]
list_six = [‘一‘, ‘WE‘, ‘哈哈‘,‘赢得了‘, ‘比赛‘, ‘!‘]
list_six.reverse() #将列表顺序翻转
print(list_six) #[‘!‘, ‘比赛‘, ‘赢得了‘, ‘哈哈‘, ‘WE‘, ‘一‘]
标签:end ever 倒数 大小写 lis 修改 调用 sort false
原文地址:https://www.cnblogs.com/surenliu/p/12589832.html