#######################list 类 列表######################## li = [1, 2, 343, ‘dfas‘, [‘fsad‘, ‘dfas‘], True] # list 是一个集合 能放任何东西进去 # print(li[0:3]) # list 是可以修改的 # 索引法 # li[1] # a = li[4][0] value = ‘fsad‘ # 切片法 # li[3:5] = [3223,3432] # 列表中的元素是以,分割 一个一个是整体 # li[1] = ‘fdasdf‘ # print(li) # in 操作 # for 循环 while循环 tuple # 删除 # del li[1] list #####转换 # 字符串转化为列表 li = list[‘dfsadfasdf‘] #若列表里只有字符串 ‘‘‘li = [‘dfas‘,‘fdsaf‘] v = "".join(li) print(v) ‘‘‘ #列表里不只有字符串 可以使用for循环 ‘‘‘ s = "" for itm in li: s = s + str(itm) print(itm) ‘‘‘ # list类中提供的方法 ‘‘‘ l、li.append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -> None -- append object to end """ v = li.append(5) 原值最后追加 v的value 位none li.append([‘‘,‘‘]) 2、清空列表 li.clear() 3、拷贝 count(self, value): # real signature unknown; restored from __doc__ L.count(value) -> integer -- return number of occurrences of value 4、计数 count(self, value): # real signature unknown; restored from __doc__ L.count(value) -> integer -- return number of occurrences of value 5、扩展原列表 参数为可迭代对象:字符串、列表框、元组 注意和append的区别 extend(self, iterable): # real signature unknown; restored from __doc__ L.extend(iterable) -> None -- extend list by appending elements from the iterable li = [112,232,3213,3] li.append([213,3213]) print(li) [112,232,3213,3,[213,3213]] li = [112,232,3213,3] li.extend([213,3213]) print(li) [112,232,3213,3,213,3213] 6、根据索引值获取位置 index index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. 7、在指定索引位置插值 insert insert(self, index, p_object): # real signature unknown; restored from __doc__ L.insert(index, object) -- insert object before index 8、删除索引对应的值 不指定索引默认最后一个值 并获取删除值 pop(self, index=None): # real signature unknown; restored from __doc__ L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. li = [112,232,3213,3] v = li.[1] print(v,li) 232 [112,3213,3] 9、选择删除值 remove remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. """ # pop remove del li[0] del li[1:2] 10、反转 reverse reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ 11、排序 sort sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ reverse = False 从小到大排序 #list 有序且可以被修改 ‘‘‘ dict ###########################元组tuple######################### ‘‘‘ 元组一旦被创建 一级元素不可修改、不能被增加和删除 嵌套的列表框可以修改 tuple 是有序的 可以进行索引 可以进行切片 可以进行for 循环 while循环 最好创建的时候加一个小逗号 tup = (fasdf,fdsfdfd,) tu = (dfas,erw,rewr,[(33,44)],4234) #获取33 v = tu[3][0][0] print(v) 33 b = tu[3][0] =234 print(b) tu = (dfas,erw,rewr,[234],4234) #############list方法 1、计数 count count(self, value): # real signature unknown; restored from __doc__ """ T.count(value) -> integer -- return number of occurrences of value """ return 0 2、找寻 index index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ ‘‘‘ ############################字典 ############# ‘‘‘ info = {"k1":v1,"k2":"v2"} #"k1":"v1" 键值对 key:value value 可以为任意值 key : 数字、字符串、元组 都可以 列表不可以 哈希表 #索引使用key值 自己申明的 #无序状态无法切片 #删除 del info[key] # for 循环 默认只循环key for item in info.keys for item in info.values for item in info .items 键值对 ####or for item in info.keys print(item,info[item]) True 1 False 0 作为key 可能与1 重复 ‘‘‘ ####################dict 常用方法 ‘‘‘ 1、dict() 2、clear(self): # real signature unknown; restored from __doc__ """ D.clear() -> None. Remove all items from D. """ pass 3、 copy(self): # real signature unknown; restored from __doc__ """ D.copy() -> a shallow copy of D """ pass 4、@staticmethod # known case直接写个类名就可以了 静态方法 fromkeys(*args, **kwargs): # real signature unknown 第一个参数为key 第二个参数为value """ Returns a new dict with keys from iterable and values equal to value. """ 创建字典,指定统一的值 pass ####5、get(self, k, d=None): # real signature unknown; restored from __doc__ """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ 根据key取value值 key不存在时返回d值 d默认值为none 6、pop(self, k, d=None): # real signature unknown; restored from __doc__ """ D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised 删除值 若key值不存在返回d 7、popitem(self): # real signature unknown; restored from __doc__ """ D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """随机删除一对键值对 8、setdefault(self, k, d=None): # real signature unknown; restored from __doc__ """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ 设置值:如果已经存在的key 不设置 获取原对应的value 若不存在 设置该value 9、update(self, E=None, **F): # known special case of dict.update """ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] """ 已经存在 覆盖掉 不存在 赋值 dic.update(k1=123,k3=234,k5="dfas") 自动转换成字典 ‘‘‘ ###############################大整理##################### ‘‘‘ #数字 int #字符串 replace find join strip startwith endwith upper lower format 特殊的 v = template.format(**{name:‘fdsa‘,age:‘fdasf‘}) #列表 append extend insert 索引、切片、循环 #元组 忽略 #字典 get update keys values items for 索引 #布尔值 0 1 bool() 为假的:none () 0 ‘‘ [] {} ‘‘‘