数列的一些增删改查操作
import copy names = ["aa","bb","cc","dd"] print(names) print(names[1],names[2]) print(names[0:2])#切片,要头不要尾 print(names[:2])#和上一个结果一样 print(names[-1])#切片,取倒数第一个 print(names[-2])#切片,取倒数第二个 print(names[-2:])#切片,省略后面的 print("\n") names.append("ee")#在末尾增加一个 print(names) names.insert(1,"ee")#插在那位数的后面 print(names) names.insert(3,"ff") print(names) names[2] = "gg"#修改某个数 print(names) names.remove("cc") print(names) del names[1]#删除某个数 print(names) names.pop(1)#删除某个数,没写默认删除最后一个 print(names) print(names.count("ee"))#计算ee的个数 names.reverse()#排序反转 print(names) names.sort()#按字母排序 print(names) print("\n") names2 = ["qqjjhh"] names.extend(names2)#把names2合拼到names print(names) print("\n") names3 = copy.deepcopy(names)#names3复制namesprint(names3)
结果显示如下
[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘] bb cc [‘aa‘, ‘bb‘] [‘aa‘, ‘bb‘] dd cc [‘cc‘, ‘dd‘] [‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘, ‘ee‘] [‘aa‘, ‘ee‘, ‘bb‘, ‘cc‘, ‘dd‘, ‘ee‘] [‘aa‘, ‘ee‘, ‘bb‘, ‘ff‘, ‘cc‘, ‘dd‘, ‘ee‘] [‘aa‘, ‘ee‘, ‘gg‘, ‘ff‘, ‘cc‘, ‘dd‘, ‘ee‘] [‘aa‘, ‘ee‘, ‘gg‘, ‘ff‘, ‘dd‘, ‘ee‘] [‘aa‘, ‘gg‘, ‘ff‘, ‘dd‘, ‘ee‘] [‘aa‘, ‘ff‘, ‘dd‘, ‘ee‘] 1 [‘ee‘, ‘dd‘, ‘ff‘, ‘aa‘] [‘aa‘, ‘dd‘, ‘ee‘, ‘ff‘] [‘aa‘, ‘dd‘, ‘ee‘, ‘ff‘, ‘qqjjhh‘] [‘aa‘, ‘dd‘, ‘ee‘, ‘ff‘, ‘qqjjhh‘]
#排序反转
原文地址:https://www.cnblogs.com/qjhh/p/12333765.html
时间: 2024-10-10 21:00:44