Python语言学习之字母R开头函数使用集锦:random/range/replace/reshape用法之详细攻略
生活随笔
收集整理的這篇文章主要介紹了
Python语言学习之字母R开头函数使用集锦:random/range/replace/reshape用法之详细攻略
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Python語言學(xué)習(xí)之字母R開頭函數(shù)使用集錦:random/range/replace/reshape用法之詳細攻略
?
?
?
?
?
?
?
?
?
?
?
?
random/range/replace/reshape用法
1、random用法
numpy.random.randint(a, b) ? # 返回開區(qū)間 [a, b) 范圍內(nèi)的整數(shù)值 ?[python]?>>> random.randint(0,1) ? ? ? ?0 ?? >>> random.randint(0,1) ? ? ? ? 1 ?>>> np.random.randint(0,1) ? 0 ?? >>> np.random.randint(0,1) ? ?0?
2、range(start, end, step)函數(shù)使用方法
print(list( range(0, 10, 3) )) ?#輸出[0, 3, 6, 9] random.randint(a, b) ? ?# 返回閉區(qū)間 [a, b] 范圍內(nèi)的整數(shù)值?
3、replace用法
replace() ? ? ? ? ? ? ? ? ? ? ?#方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個參數(shù)max,則替換不超過 max 次。 (1)、my_file_path.replace(u'/',u'\\') ?#將文件路徑中的/改為\(第一個是轉(zhuǎn)義符) reversed() ? ? ? ? ? ? ? ? ? ?#序列值上的反向迭代器 (2)replace('\t','') ? ? #該函數(shù)將原來列表中,元素有含\t統(tǒng)統(tǒng)去掉(1)、replace函數(shù):for循環(huán)去掉sources列表內(nèi)的\t字符
#for循環(huán)去掉sources列表內(nèi)的\t字符:例如['網(wǎng)絡(luò)媒體', ……, '網(wǎng)游', '\t網(wǎng)絡(luò)媒體', '\t汽車'] → ['網(wǎng)絡(luò)媒體', ……, '網(wǎng)游', '網(wǎng)絡(luò)媒體', '汽車'] pureLists=[] for i in range(len(sources)): pureList=sources[i].replace('\t','')pureLists.append(pureList) print(pureLists) print(sum(wealths))?
4、reshape用法
reshape(FN,-1)? ? ? ? #通過reshape(FN,-1) 將參數(shù)指定為-1,這是reshape的一個便利的功能通過在reshape時指定為-1,reshape函數(shù)會自動計算-1維度上的元素個數(shù),以使多維數(shù)組的元素個數(shù)前后一致。
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Python语言学习之字母R开头函数使用集锦:random/range/replace/reshape用法之详细攻略的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python语言学习之字母S开头函数使用
- 下一篇: Python语言学习之字母A开头函数使用