python - 如何在 Python 中为 boucle 做随机数?
问题描述
有没有办法在 Python 中随机化 for boucle?
像这样的东西:
for i in random(range(0,5)): #This is an example and not working at all
print(i)
-> 并且结果应该是例如:
4
2
3
1
0
解决方案
这有效:
import random
items = [x for x in range(0,5)] # create a list with items
random.shuffle(items) # shuffle it (in-place)
for i in items: # iterate over
print(i) # print
请random
参阅相应的文档。
推荐阅读
- macros - 在 Scheme Hygienic Macros 中取消引用拼接等效项
- nginx - LinkedIn 后检查员给出 500 次失败
- android - 如何在计算着色器中最好地访问 ARCore 视频帧
- python - 编写一个接受数字列表和单个数字作为参数的函数。该函数应打印所有小于数字的数字
- python - 在 python 中创建确定性的 UUID 集
- sql - sql server中基于join的转置
- google-sheets - 谷歌表格,条件格式,如果“旁边”的单元格具有相同的值,请检查每个单元格
- android - SimpleDateFormat 未应用 TimeZone
- php - 我有 errir like Notice: Trying to get property of non-object in C:\xampp\htdocs\exapmle\index.php on line 27
- python - 嵌套循环帮助python中的子集数据