網(wǎng)站首頁 編程語言 正文
函數(shù)用法
??random.shuffle()用于將一個列表中的元素打亂順序,值得注意的是使用這個方法不會生成新的列表,只是將原列表的次序打亂。
代碼案例
# shuffle()使用樣例
import random
x = [i for i in range(10)]
print(x)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random.shuffle(x)
print(x)
[2, 5, 4, 8, 0, 3, 7, 9, 1, 6]
源碼及注釋
def shuffle(self, x, random=None):
"""Shuffle list x in place, and return None.
原位打亂列表,不生成新的列表。
Optional argument random is a 0-argument
function returning a random float in [0.0, 1.0);
if it is the default None,
the standard random.random will be used.
可選參數(shù)random是一個從0到參數(shù)的函數(shù),返回[0.0,1.0)中的隨機浮點;
如果random是缺省值None,則將使用標準的random.random()。
"""
if random is None:
randbelow = self._randbelow
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = randbelow(i + 1)
x[i], x[j] = x[j], x[i]
else:
_int = int
for i in reversed(range(1, len(x))):
# pick an element in x[:i+1] with which to exchange x[i]
j = _int(random() * (i + 1))
x[i], x[j] = x[j], x[i]
參考文獻
[1]python中random.shuffle 使用
[2]Python中打亂列表順序 random.shuffle()的使用方法
原文鏈接:https://blog.csdn.net/zfhsfdhdfajhsr/article/details/110290921
相關推薦
- 2022-01-29 win server 2008 web IIS部署asp.net程序后,CSS樣式錯亂不顯示問題
- 2023-07-18 SpringBoot中無法用@Value獲取yml中的配置值的原因
- 2023-02-27 nginx編譯安裝及常用參數(shù)詳解_nginx
- 2023-06-03 python中with的具體用法_python
- 2022-06-02 C語言用棧模擬實現(xiàn)隊列問題詳解_C 語言
- 2022-07-28 C++中操作符的前置與后置有什么區(qū)別_C 語言
- 2022-05-02 Pyinstaller+Pipenv打包Python文件的實現(xiàn)示例_python
- 2022-11-06 Go+Redis實現(xiàn)延遲隊列實操_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支