当前位置: 首页 > 图灵资讯 > 行业资讯> python中random模块将列表内容打乱

python中random模块将列表内容打乱

发布时间:2024-10-09 15:50:48

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。

1、shuffle() 方法

随机排序序列中的所有元素,以打乱序列

2、random.函数原型shuffle

random.shuffle(x[,random])

3、random.shuffle() 方法的语法

importrandom

random.shuffle(lst)

4、参数

lst – 可以是列表

5、返回值

6、实例:使用random.shuffle打乱列表内容
#-*-coding:UTF-8-*-
importrandom

#打乱排序
li=[0,1,2,3,4,5,6,7,8]
random.shuffle(li)
print(li)

注:支持列表,不支持字符串

以上就是python中random.shuffle()函数打乱列表的原始和操作过程,希望对您有所帮助~

相关文章

python3兼容python2吗

python3兼容python2吗

2025-05-09
python3 whl怎么安装

python3 whl怎么安装

2025-05-09
python 字典怎么提取value

python 字典怎么提取value

2025-05-09
python 怎样计算字符串的长度

python 怎样计算字符串的长度

2025-05-09
python 怎么样反向输出字符串

python 怎么样反向输出字符串

2025-05-09
python 怎么判断字符串开头

python 怎么判断字符串开头

2025-05-09