python一些知识碎片3

一、指定长度数组

[0] * 10

二、减少加法运行速度,可以用内置函数库

sum(range(1,100000000))

三、字典的合并

1.
dict1 = {"one": 1, "two": 2, "three": 3}
dict2 = {"four": 4, "five": 5}
dict1.update(dict2)
print(dict1)
2.
print(dict(dict1, **dict2))

四、版本

import sys
s = sys.version
print(s[:5], type(s))

五、魔法方法

print(random.__all__)
__name__
__doc__

六、构造字典列表

list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
list3 = [7, 8, 9, 1, 2]
z1 = zip(list1, list2)
z2 = zip(list3, z1)
print(dict(z2))

七、tuple比list效率高,list比set效率高

八、星的用法

dict1 = {"one": 1, "two": 2, "three": 3}
def fun(x, y, z):
    print(x * 3, y, z)
fun(*dict1)

九、字典函数

dict1 = {
    "addxy": lambda x, y: x + y,
    "mulxy": lambda x, y: x * y
}
print(dict1["mulxy"](20, 20))

你可能感兴趣的:(python一些知识碎片3)