一些骚操作

  • 列表转字典
l = [5,4,3,2,1]
dic = {i:v for i,v in enumerate(l)}
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5}
  • 三元运算
    res = 1 if 2 <3 else 3如果条件成立,res就为1,否则就为3
my_lsit = [1,2]
print(my_lsit[5])
将会报错
![](https://upload-images.jianshu.io/upload_images/11743438-2e60ab00137a5951.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

如果使用
my_lsit[5:]
将不会报错,将会返回一个新的list  [] 返回一个空列表

安装包解决https限制
在后面加上
-i https://pypi.douban.com/simple/ --trusted-host pypi.douban.com
也可以在.pip源中直接配置
在pypi上下载的py包,有时候是xxx.tar.gz2格式的,也可以直接使用 pip install xxx.tar.gz2来安装,
场景是在安装 twisted包。

链式判断

and or 来判断取值挺爽的

类中的参数书写

还有一些方法的书写
image.png

在字符串的拼接中,除了sql中用%占位之外,其他的字符串拼接方式尽量不要使用% 和 + 这样的方式,避免变量类型不同引起的报错。最好使用format来进行拼接,这样可以不用考虑变量类型。

format的操作

filter

a = [1,2,'sdas',True,set(),list(),dict()]
print(a)
# [1, 2, 'sdas', True, set(), [], {}]
b = list(filter(bool,a))
for i in b:
    print(i)

b = (1,2,'sdas',True)
# 过滤出所有的bool值
# 1
# 2
# sdas
# True

posted @ 2019-09-07 16:54  π=3.1415926  阅读(135)  评论(0编辑  收藏  举报