新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python隨機數(shù)/Python隨機字符串的相關(guān)代碼解析
以下的文章是通過Python隨機數(shù)與Python隨機字符串在實際應用中的代碼的形式來介紹Python隨機數(shù)與Python隨機字符串的實際應用的相關(guān)介紹,以下是文章的具體介紹,下面讓我們一起分享吧!

Python隨機數(shù)與隨機字符串
- >>> import random
- >>> random.randint(0,99)
- 21
隨機選取0到100間的偶數(shù):
- >>> import random
- >>> random.randrange(0, 101, 2)
- 42
隨機浮點數(shù):
- >>> import random
- >>> random.random()
- 0.85415370477785668
- >>> random.uniform(1, 10)
- 5.4221167969800881
隨機字符:
- >>> import random
- >>> random.choice('abcdefg%^*f')
- 'd'
多個字符中選取特定數(shù)量的字符:
- >>> import random
- random.sample('abcdefghij',3)
- ['a', 'd', 'b']
多個字符中選取特定數(shù)量的Python隨機字符字符串:
- >>> import random
- >>> import string
- >>> string.join(random.sample(['a','b','c','d','e','f','g','h','i','j'], 3)).r
- eplace(" ","")
- 'fih'
隨機選取字符串:
- >>> import random
- >>> random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )
- 'lemon'
洗牌:
- >>> import random
- >>> items = [1, 2, 3, 4, 5, 6]
- >>> random.shuffle(items)
- >>> items
- [3, 2, 5, 6, 4, 1]
以上的文章就是對Python隨機數(shù)與Python隨機字符串的實際應用代碼的介紹。
【編輯推薦】
- Python字符和字符串的相關(guān)代碼示例解析
- Python對象以C語言實現(xiàn)其相關(guān)功能的介紹
- Python VIM 注釋掉代碼的具體方案的描述
- ?Python手工加載Django的實際操作的四部分詳解
- Python安裝中對配置環(huán)境變量實際操作的方法
網(wǎng)頁標題:Python隨機數(shù)/Python隨機字符串的相關(guān)代碼解析
URL鏈接:http://www.dlmjj.cn/article/dpspjoh.html


咨詢
建站咨詢
