# list = ["alex",[1,2,3],"egon","女神","taibai"] # #切片操作,与字符串操作一样 # print(list[0]) # print(list[1]) # print(list[0:3]) # 列表增加元素 : 三种操作 # append:在后面追加。 # insert:通过索引插入到该索引元素的后面 # extend: 迭代插入,必须是可迭代的,整形不可插入,可迭代的对象会分解成每个元素 #对于列表与字符串的不同点:字符串的修改不在原字符串上,列表则在原列表上修改 # list = ["alex",[1,2,3],"wusir","egon","女神","taibai"] # print(list) # # list1 = [1,2,3,4,5,6,7,8,9,10] # # i = 0 # # while True: # # username = input("是否输入员工的名字:y/q") # # if username.strip().lower() == "y": # # list.append(list1[i]) # # elif username.strip().lower() == "q": # # print(list) # # break # # else: # # print("请重新输入") # # i+=1 # list.insert(4,"春哥") # print(list) # # list.extend("二哥") # print(list) # # list.extend([1,2,3]) # print(list) #列表删除操作:四种操作 # pop:根据索引删除,并且可以返回返回值 # remove:按元素去删除 # clear:清空列表 # del: 删除列表,可以进行切片删除 #pop操作 # list = ["taibai","alex","wusir","egon","女神"] # name = list.pop(1) #返回值 # name1 = list.pop() #默认删除最后一个 # print(name) # print(list) #remove操作 # list = ["taibai","alex","wusir","egon","女神"] # list.remove("taibai") # print(list) #clear操作 # list = ["taibai","alex","wusir","egon","女神"] # list.clear() # print(list) #del操作 # list = ["taibai","alex","wusir","egon","女神"] # del list[0:3] #切片删除 # print(list) # del list # print(list) #列表修改操作 #按索引直接去改 # list = ["taibai","alex","wusir","egon","女神"] # list[0] = "男神" # print(list) # list[1] = [1,2,3] # print(list) # # list[0:2] = "云姐pl" #切片修改,拿几个去掉,不管写多少,都按照迭代去修改 # list[0:3] = [1,2,3,"春哥","hhh"] #按照列表修改。则依旧按列表改 # print(list) #列表查找操作 # list = ["taibai","alex","wusir","egon","女神"] # for i in list: # print(i) # # print(list[0:2]) #字符串,列表,字典:公共方法 # list = ["taibai","alex","wusir","egon","女神"] # l = len(list) #求出列表的长度 # print(l) # # li = list.count("alex") #求出出现的次数 # print(li) # # #查找索引 列表中只有index,若没有找到就报错!!! # print(list.index("alex")) #列表的排序方法。sort() # list = [1,5,6,9,7,2,3,5] # # list.sort() #正向排序 # # print(list) # print(list.sort(reverse=True)) #反向排序 # # #反转 # li = list.reverse() # print(li) #列表的嵌套 # list = ["taibai","wusir","袁浩",["alex","egon",89],23] # # print(list[2][1]) # # list[0] = list[0].capitalize() # # print(list) # # list[2] = list[2].replace("浩","fxfs") # # print(list) # # list[2] = "袁田" # # print(list) # # list[3][0] = list[3][0].upper() # print(list)
关于Python的列表操作
最新推荐文章于 2024-11-10 13:43:20 发布