美文网首页python总结
python技巧积累

python技巧积累

作者: ciferlv | 来源:发表于2017-11-24 17:17 被阅读0次

数组逐行替换

import numpy as np

init_matrix = np.random.uniform(-1,1,[2,3])
test_map = map(lambda row : row / 2, init_matrix)
#下面这句话是python2的语法
#python3使用: test_matrix = np.array(list(test_map))
test_matrix = np.array(test_map)
print(init_matrix)
print(test_matrix)
init_matrix:
array([[-0.48006762,  0.6910708 ,  0.53442369],
       [ 0.60233271,  0.40702649, -0.70795476]])
test_matrix:
array([[-0.24003381,  0.3455354 ,  0.26721185],
       [ 0.30116635,  0.20351324, -0.35397738]])

生成多维数组,元素随机

import numpy as np
#[0,10),左闭右开,整数
np.random.randint(0,10,size=[3,3])

array([[4, 8, 2],
       [5, 3, 6],
       [5, 9, 5]])

#[0.0,1.0)
np.random.random(size=[3,3])

array([[ 0.54587373,  0.43366728,  0.91261908],
       [ 0.3425949 ,  0.83666684,  0.5742228 ],
       [ 0.29507124,  0.16803488,  0.04462426]])

dict按照key排序生成list、dict按照value排序生成list

相关文章

网友评论

    本文标题:python技巧积累

    本文链接:https://www.haomeiwen.com/subject/gkefbxtx.html