分享

Python语言学习之字母R开头函数使用集锦:random/range/replace/reshape用法之详细攻略

 处女座的程序猿 2021-09-28

Python语言学习之字母R开头函数使用集锦:random/range/replace/reshape用法之详细攻略

random/range/replace/reshape用法

1、random用法

numpy.random.randint(a, b)   # 返回开区间 [a, b) 范围内的整数值  [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)函数使用方法

print(list( range(0, 10, 3) ))  #输出[0, 3, 6, 9]
random.randint(a, b)    # 返回闭区间 [a, b] 范围内的整数值

3、replace用法

replace()                      #方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
(1)、my_file_path.replace(u'/',u'\\')  #将文件路径中的/改为\(第一个是转义符)
reversed()                    #序列值上的反向迭代器
(2)replace('\t','')     #该函数将原来列表中,元素有含\t统统去掉

(1)、replace函数:for循环去掉sources列表内的\t字符

#for循环去掉sources列表内的\t字符:例如['网络媒体', ……, '网游', '\t网络媒体', '\t汽车'] → ['网络媒体', ……, '网游', '网络媒体', '汽车']
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) 将参数指定为-1,这是reshape的一个便利的功能通过在reshape时指定为-1,reshape函数会自动计算-1维度上的元素个数,以使多维数组的元素个数前后一致。

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多