一些python function总结

今天看一份代码的时候,有些python function不知其何用,想来写个博客把每次遇到的总结在一篇blog,可以时常看看。

这个可以print出python file or function中注释部分,比如:

in module.py:

1
2
3
4
5
6
"""This is the module file, use for provide a function."""
def func(x):
return x
>>>import module
>>>print(module.__doc__)
>>>'This is the module file, use for provide a function.'

把一个list的string转成float

1
2
>>> nums = ['1.0','2.0']
>>> float_nums = list(map(float, nums))

随机数Random Module

均一分布(uniform)随机数
1
>>>random.unifrom(a, b)
两数范围内的随机整数
1
>>> random.randint(a,b)
从sequence中随机选择一个element
1
2
>>> nums = ["blue", "green"]
>>> random.choice(nums)