python的random模块常用方法讲解--大全

目的

长期做NLP研究,经常需将语料处理成特殊的格式,这个模块经常用到,每次都要去查。。。。。今天有时间,一次性,整理出中文的所有的方法的解释和例子,方便自己查阅,同时分享出来供大家参考
好了废话不说了…

整数领域

random.randrange(start,stop[,step])

作用

range(start,stop,step),随机选择一个元素(注意:实际上没有创建一个range object.)

例子
#例子1
print(random.randrange(5))
#例子2
r_1=random.randrange(0,6,2)
r_2=random.randrange(1,6,2)
print("r_1",r_1)
print("r_2",r_2)

输出:

0
r_1:  0
r_2:  1

random.randint(a, b)

作用

在区间[a,b]中随机的返回一个整数

例子
#例子1
print("在[0,5]之间随机的选取一个整数: ",random.randint(0,5))

输出:

在[0,5]之间随机的选取一个整数:  1

序列领域

random.choice(seq)

作用

在一个 非空的序列(注意序列的结构不能是字典) 中随机选取一个元素

例子
seq_list=[1,3,45,67,88,1]
seq_dict={"1":1,"2":2,"3":3,"4":4}
seq_tuple=(1,34,56,7,8,9)
seq_other=[seq_list,seq_dict,seq_tuple]


print("在一个list中随机的选择一个元素: ",random.choice(seq_list))
print("在一个tuple中随机的选择一个元素: ",random.choice(seq_tuple))
print("在一个嵌套的序列中随机的选择一个元素: ",random.choice(seq_other))

#会报错,是因为字典的特殊存储结构
try:
    print("在一个dict中随机的选择一个元素: ",random.choice(seq_dict))
except Exception:
    print("当序列是词典是出错")

输出:

在一个list中随机的选择一个元素:  67
在一个tuple中随机的选择一个元素:  8
在一个嵌套的序列中随机的选择一个元素:  (1, 34, 56, 7, 8, 9)
当序列是词典是出错

*random.choices(population, weights=None, , cum_weights=None, k=1)

作用
  • 以有放回的方式 从populaition(注意:不能是字典类型的序列)随机抽取K个元素
  • 若指定weights(权重序列),根据相对权重进行选择。同时len(weights)必须等于len(population)
  • 若给出了一个cum_weights(累计权重序列),根据累计权重进行选择。权重cum_weights=[10,5,30,5]等权重[10,15,45,50]。
  • 若上述weights 和cum_weights都未指定,则以等概率取样
  • weights 和cum_weights 最多只能有一个赋值
  • 这个函数在python3.6版本中才有
例子
seq_list=[1,3,45,67,88,1111]
weights_list=[100000000,1,1,1,1,1]

seq_dict={"1":1,"2":2,"3":3,"4":4}
weights_dict=[10000000,1,1,1]

seq_tuple=(1,34,56,7,8,9)
weights_tuple=[10000000,1,1,1,1,1]

seq_other=[seq_list,seq_dict,seq_tuple]
weights_other=[10000000,1,1]

print("list: \n",seq_list)
print("权重: \n",weights_list)
print("在一个list中有放回的等概率的选择20个元素: \n",random.choices(seq_list,k=20))
print("在一个list中有放回的根据权重选择20个元素: \n",random.choices(seq_list,weights=weights_list,k=20))

print("\n\n")
print("tuple: \n",seq_tuple)
print("权重: \n",weights_tuple)
print("在一个tuple中有放回的等概率的选择20个元素: \n",random.choices(seq_tuple,k=20))
print("在一个tuple中有放回的根据权重选择20个元素: \n",random.choices(seq_tuple,weights=weights_tuple,k=20))

print("\n\n")
print("other: \n",seq_other)
print("权重: \n",weights_other)
print("在一个嵌套的序列中有放回的等概率的选择20个元素: ",random.choices(seq_other,k=20))
print("在一个嵌套的序列中有放回的根据权重选择选择20个元素: ",random.choices(seq_other,weights=weights_other,k=20))

#会报错,是因为字典的特殊存储结构
try:
    print("在一个dict中随机的选择一个元素: ",random.choices(seq_dict,k=1))
except Exception:
    print("当序列是词典是出错")

输出:

list: 
 [1, 3, 45, 67, 88, 1111]
权重: 
 [100000000, 1, 1, 1, 1, 1]
在一个list中有放回的等概率的选择20个元素: 
 [67, 45, 88, 1111, 67, 45, 67, 1, 3, 88, 45, 3, 88, 45, 45, 88, 3, 45, 1, 67]
在一个list中有放回的根据权重选择20个元素: 
 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
 
tuple: 
 (1, 34, 56, 7, 8, 9)
权重: 
 [10000000, 1, 1, 1, 1, 1]
在一个tuple中有放回的等概率的选择20个元素: 
 [9, 8, 56, 8, 34, 56, 9, 34, 56, 56, 1, 34, 56, 9, 8, 34, 8, 8, 34, 34]
在一个tuple中有放回的根据权重选择20个元素: 
 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
other: 
 [[1, 3, 45, 67, 88, 1111], {'1': 1, '2': 2, '3': 3, '4': 4}, (1, 34, 56, 7, 8, 9)]
权重: 
 [10000000, 1, 1]
在一个嵌套的序列中有放回的等概率的选择20个元素:  [[1, 3, 45, 67, 88, 1111], {'1': 1, '2': 2, '3': 3, '4': 4}, {'1': 1, '2': 2, '3': 3, '4': 4}, (1, 34, 56, 7, 8, 9), [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], (1, 34, 56, 7, 8, 9), {'1': 1, '2': 2, '3': 3, '4': 4}, [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], {'1': 1, '2': 2, '3': 3, '4': 4}, [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], (1, 34, 56, 7, 8, 9), [1, 3, 45, 67, 88, 1111], {'1': 1, '2': 2, '3': 3, '4': 4}, {'1': 1, '2': 2, '3': 3, '4': 4}]
在一个嵌套的序列中有放回的根据权重选择选择20个元素:  [[1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111], [1, 3, 45, 67, 88, 1111]]
当序列是词典是出错

​

random.shuffle(x[, random])

作用

随机打乱序列X

  • x 不能是词典:词典本身就是无序的(详情,请了解字典的存储结构)
  • x不能是tuple(元组的元素不能修改。)
  • 注意:是在原序列上进行打乱的
例子
seq_list=[1,3,45,67,88,1111]
seq_dict={"1":1,"2":2,"3":3,"4":4}
seq_tuple=(1,34,56,7,8,9)
seq_other=[seq_list,seq_dict,seq_tuple]

print("list: ",seq_list)
random.shuffle(seq_list)
print("打乱list序列: ",seq_list)


print("\n\n")
print("other: ",seq_other)
random.shuffle(seq_other)
print("打乱嵌套的序列: ",seq_other)


#会报错
try:
    random.shuffle(seq_tuple)
except Exception:
    print("打乱tuple序列失败,原因是:元组的元素不能修改")



#会报错,是因为字典的特殊存储结构
try:
    random.shuffle(seq_dict)
except Exception:
    print("当序列是词典是出错")

输出:

list:  [1, 3, 45, 67, 88, 1111]
打乱list序列:  [67, 1, 45, 3, 88, 1111]


other:  [[67, 1, 45, 3, 88, 1111], {'1': 1, '2': 2, '3': 3, '4': 4}, (1, 34, 56, 7, 8, 9)]
打乱嵌套的序列:  [(1, 34, 56, 7, 8, 9), [67, 1, 45, 3, 88, 1111], {'1': 1, '2': 2, '3': 3, '4': 4}]


打乱tuple序列失败,原因是:元组的元素不能修改
当序列是词典是出错

random.sample(population, k)

作用

返回从population中以 不放回抽取方式 选择k个元素

  • 注意:不改变原序列
  • Population必须是一个序列 or 集合 。For dicts, use list(d)[选择的是 key]
例子
seq_list=[1,3,45,67,88,1111]
seq_dict={"1":1,"2":2,"3":3,"4":4}
seq_tuple=(1,34,56,7,8,9)
seq_other=[seq_list,seq_dict,seq_tuple]


print("list: ",seq_list)
print("从list序列抽取3个: ",random.sample(seq_list,3))
print("\n")


print("tuple: ",seq_tuple)
print("从tuple序列抽取3个: ",random.sample(seq_tuple,3))
print("\n")



print("other: ",seq_other)
print("从嵌套序列抽取3个: ",random.sample(seq_other,2))
print("\n")



print("dict: ",seq_dict)
try:
    print("从dict序列抽取3个: ",random.sample(seq_dict,3))
except Exception:
    print("直接使用词典会报错")
    print("从dict序列抽取3个key: ",random.sample(list(seq_dict),3))
    

输出:

list:  [1, 3, 45, 67, 88, 1111]
从list序列抽取3个:  [1, 88, 3]

tuple:  (1, 34, 56, 7, 8, 9)
从tuple序列抽取3个:  [1, 9, 7]

other:  [[1, 3, 45, 67, 88, 1111], {'1': 1, '2': 2, '3': 3, '4': 4}, (1, 34, 56, 7, 8, 9)]
从嵌套序列抽取3个:  [{'1': 1, '2': 2, '3': 3, '4': 4}, (1, 34, 56, 7, 8, 9)]

dict:  {'1': 1, '2': 2, '3': 3, '4': 4}
直接使用词典会报错
从dict序列抽取3个key:  ['4', '1', '2']

产生真实的分布Real-valued distributions

random.random()

作用

在[0.0,1.0)产生一个zfloat类型的数

例子
print(random.random())

输出:

0.05863293760200872

不常用的函数这里就不讲解了,有兴趣的同学点击自己看哈
random.uniform(a, b)
random.triangular(low, high, mode)
random.normalvariate(mu, sigma)

猜你喜欢

转载自blog.csdn.net/qq_32806793/article/details/85270141