美文网首页
Python 基础知识巩固

Python 基础知识巩固

作者: shirely大丫头 | 来源:发表于2019-04-21 23:01 被阅读0次

https://blog.csdn.net/ctyct_/article/details/79677125
单例模式
2.list的删除方法:remove pop del
remove是针对可变列表的元素进行搜索删除
remove(item)方法是直接对可变序中的元素进行检索删除,返回的是删除后的列表,不返回删除值(返回None)
pop(index)方法是对可变序列中元素下标进行检索删除,返回删除值
del(list[index])方法是对可变序列中元素下边进行检索删除,不返回删除值
3.Python中异常信息:
IOError:输入输出异常
AttributeError:试图访问一个对象没有的属性
ImportError:无法引入模块或包,基本是路径问题
IndentationError:语法错误,代码没有正确的对齐
IndexError:下标索引超出序列边界
KeyError:试图访问你字典里不存在的键
SyntaxError:Python代码逻辑语法出错,不能执行
NameError:使用一个还未赋予对象的变量

4.sort和sorted对列表排序
list.sort(reverse=False) list.sort在list 基础上修改,无返回值
sorted(list,reverse=False) sorted返回值是新的list,原来的list不变
sort方法还有两个可选参数:key和reverse
1、key在使用时必须提供一个排序过程总调用的函数:

x = ['mmm', 'mm', 'mm', 'm' ]
x.sort(key = len)
print (x) # ['m', 'mm', 'mm', 'mmm']
2、reverse实现降序排序,需要提供一个布尔值:

y = [3, 2, 8 ,0 , 1]
y.sort(reverse = True)
print (y) #[8, 3, 2, 1, 0]
True为倒序排列,False为正序排列

python items() 函数以列表返回可遍历的(键, 值) 元组数组
3、
import json
dic ={"naem":"zs"}
res=json.dumps(dic) #字典转json字符串
print(res,type(res)) <class 'str'>
ret=json.loads(res) #json转字典
print(ret,type(ret)) <class 'dict'>

4 统计字符串中某字符出现的次数
res=str.count("张三")
upper() 字符串大写
lower() 字符串小写

相关文章

网友评论

      本文标题:Python 基础知识巩固

      本文链接:https://www.haomeiwen.com/subject/mgyjgqtx.html