Python中文
首页
教程
问答
标签
搜索
登录
注册
基于for循环中的概率打印列表元素
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>基于p的概率,如果p<;0.5,我想取列表1对应位置的字母。你知道吗</p> <p>例如:</p> <pre><code>for i in range(5): list1 = ['A', 'B', 'C', 'D', 'E'] p = np.random.uniform(low= 0.0, high= 1.0, size=5) print(p) </code></pre> <p>输出为:</p> <pre><code>[ 0.46565909 0.741431 0.65590764 0.87347741 0.38465195] [ 0.62172525 0.80688763 0.40391766 0.28042554 0.34544989] [ 0.00138961 0.56959351 0.69043625 0.59473154 0.84042555] [ 0.18535428 0.63470281 0.27882709 0.78731892 0.63624727] [ 0.89383216 0.72008758 0.66048462 0.94064897 0.1484418 ] </code></pre> <p>所以根据概率,我希望我的输出是:</p> <pre><code>['A', 'E'] ['C', 'D', 'E'] ['A'] ['A', 'C'] ['E'] </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>只是另一种选择:</p> <pre><code>[ [l for r, l in zip(np.random.uniform(low= 0.0, high= 1.0, size=5), list1) if r > 0.5] for i in range(5) ] #=> [['A'], ['D', 'E'], ['B', 'C'], ['D'], ['B', 'C', 'E']] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何用if条件捕获函数返回值
3 回答
如何用if语句判断列表中是否存在该索引?
5 回答
如何用if语句向量化numpy数组中的最大值?
3 回答
如何用IF语句有条件地保存零碎的结果?
7 回答
如何用if语句测试异常对象?
7 回答
如何用IF语句编写二元函数
8 回答
如何用igraph在python中创建顶点权重的图?
9 回答
如何用ijson和python解析json
6 回答
如何用iloc求子矩阵
6 回答
如何用Imagemagick或PIL绘制高质量的图像笔划(边框)?
8 回答
如何用importlib在python中动态导入模块?
9 回答
如何用import语句重写python内置函数?
3 回答
如何用imshow混合裁剪的强度并显示正确的混合强度?
3 回答
如何用in dictionary解析havin dictionary中的json文件
6 回答
如何用in-Django URL替换%20
5 回答
如何用in\op正确构造查询
5 回答
如何用inbuild对象替换文件
4 回答
如何用inheritan类实现flask restful
5 回答
如何用intersphinx正确地编写对外部文档的交叉引用?
5 回答
如何用int修改LpVariable?
7 回答