python按照正态分布从列表中选择一个元素
2022/6/11 1:20:21
本文主要是介绍python按照正态分布从列表中选择一个元素,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
我想按照正态分布使用python从列表中选择一个元素。我有一个清单,例如
alist = ['an', 'am', 'apple', 'cool', 'why']
例如,根据正态分布的概率密度函数(PDF),给定列表中的第3个元素应该被选择为最大概率。
from random import normalvariate defnormal_choice(lst, mean=None, stddev=None): if mean is None: # if mean is not specified, use center of list mean = (len(lst) - 1) / 2 if stddev is None: # if stddev is not specified, let list be -3 .. +3 standard deviations stddev = len(lst) / 6 while True: index = int(normalvariate(mean, stddev) + 0.5) if 0 <= index < len(lst): return lst[index]
然后
alist = ['an', 'am', 'apple', 'cool', 'why'] for _ in range(20): print(normal_choice(alist))
给
why an cool cool cool apple cool apple am am apple apple apple why cool cool cool am am apple 转载:https://qa.1r1g.com/sf/ask/2483072301/
这篇关于python按照正态分布从列表中选择一个元素的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python