3.元组的常用方法
t = (1,2,3,4,5,6,55,55,7,7,5,87,2) #count方法 print(t.count(2)) #index方法 print(t.index(2)) #下标index方法调用注意如果不存在的下标就会报错 print(t.index(10))
四、列表
1.列表的定义和下标访问
cl1 = [] print(cl1) print(type(cl1)) c1 = [1] print(c1) print(type(c1)) #定义具有多个元素的列表 cl3 = [1,2,3,'hello',(4,5,6),['a','b','c']] print(cl3) print(type(cl3)) for v in cl3: if isinstance(v,tuple) or isinstance(v,list) or isinstance(v,str): for i in v: print(i) else: print(v)
用list()创建列表对象
cl4 = list() print(cl4) print(type(cl4)) #放入的数据需要可迭代 cl5 = list('hahaha') print(cl5) print(type(cl5))
通过下表访问列表中的元素
cl6 = [1,2,3,4,5] print(cl6[0]) print(cl6[1]) print(cl6[2]) print(cl6[3]) print(cl6[4])
2.列表的排序和逆序
逆序方法1,逆序字符串
s = 'hello' print(s) def revers_str(s): ret_s = "" i = len(s) - 1 #这里考虑用insert但是没有发现有 while i >= 0: ret_s += s[i] i -= 1 return ret_s s = revers_str(s) print(s)
排序和逆序集成函数
c1 = [9,2,5,7,1,8,4,3,0,6] print(c1) #排序默认从小到大 c1.sort() print("sort1: ",c1) #逆序/降序 c1.reverse() print("reverse1: ",c1) c1 = [9,2,5,7,1,8,4,3,0,6] #降序排序(从大到小) print(c1) c1.sort(reverse=True) print("sort2: ",c1) #逆序是直接将原列表中的顺序进行逆序的 c1.reverse() print("reverse2: ",c1)
c1 = [9,2,5,7,1,8,4,3,0,6] print(c1) def reverse_list(c1): ret_1 = [] i = len(c1) -1 while i >= 0: ret_1.append(c1[i]) i -= 1 #ret_1不是全局变量,所以返回 return ret_1 ret = reverse_list(c1) print(ret)
3.列表常用方法
3.1 增
c1 = [] #添加不同类型 c1.append(1) c1.append(2) c1.append(3) c1.append('hello') print(c1) #添加列表 c1.append(['a','b','c']) print(c1) print("_______________________________________________________________") #extend()扩展,append添加对象会出现多维的现象 cl1 = [1,2,3] cl2 = ['a','b','c'] cl1.append(cl2) print(cl1) print("_______________________________________________________________") #extend不会出现多维的现象 cl1 = [1,2,3] cl2 = ['a','b','c'] cl1.extend(cl2) print(cl1) print(cl2) print("_______________________________________________________________") #insert 插入 #指定插入下标 cl3 = [1,2,3,4,5] cl3.insert(2,999) print(cl3) #超出下标上限会默认添加到最后一位,相当于append cl3 = [1,2,3,4,5] cl3.insert(8,999) print(cl3)
3.2 查
list1 = [1,2,3,4,5,6,7,6,6,6,6,6,6] #count()对元素个数统计 print(list1.count(6)) #index()返回元素第一个出现的位置 print(list1.index(6)) #in和not in print(2 in list1) print(4 not in list1)
3.3 删
list1 = [1,2,3,4,5,6,7,6,6,6,6,6,6] #pop返回值是删除的元素 l1 = list1.pop() print(list1) print(l1) print("__________________________________________________________________") #移除特定元素 list1 = [1,2,3,4,5,6,7,6,6,6,6,6,6] l1 = list1.remove(7) print(list1) print(l1) print("_____________________________________________________________________") list1 = [1,2,3,4,5,6,7,6,6,6,6,6,6] del list1[1] print(list1) print("_____________________________________________________________________") list1 = [1,2,3,4,5,6,7,6,6,6,6,6,6] del(list1[1]) print(list1) print("_____________________________________________________________________") list1 = [1,2,3,4,5,6,7,6,6,6,6,6,6] list1.clear() print(list1) print("_____________________________________________________________________") #在使用列表时,不要在循环遍历时删除元素 list1 = [1,2,3,4,5,6,7,6,6,6,6,6,6] for o in list1: list1.remove(o) print(list1)
总结
上周一边准备去实习,搬家各种琐事都写了几万字的学习笔记,我真的太佩服我自己了!!!
点赞👍