码迷,mamicode.com
首页 > 其他好文 > 详细

numpy简单用法2

时间:2018-05-06 20:37:03      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:--   方法   copy   分享   rgs   span   nbsp   false   play   

技术分享图片
a = np.arange(12)
b = a
print (b is a)
b.shape = 3,4
print (a.shape)
print (id(a))
print (id(b))

>>>
True
(3, 4)
2052537612608
2052537612608

c = a.view()#潜复制
print (c is a)
c.shape = 2,6
print (a.shape)
c[0,4] = 1234
print (a)
print (id(a))
print (id(c))

>>>
False
(3, 4)
[[   0    1    2    3]
 [1234    5    6    7]
 [   8    9   10   11]]
1974659569504
1974659621312


d = a.copy() #深复制
print(d is a)
d[0,0] = 9999
print (d) 
print (a)
>>>
False
[[9999    1    2    3]
 [   4    5    6    7]
 [   8    9   10   11]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
复制
a = np.array([[4, 3, 5], [1, 2, 1]])
print (a)
print (--------)
b = np.sort(a, axis=1)#从小到大排序方法一
print (b)
a.sort(axis=1)#从小到大排序方法二
print (--------)
print (a)

>>>
[[4 3 5]
 [1 2 1]]
--------
[[3 4 5]
 [1 1 2]]
--------
[[3 4 5]
 [1 1 2]]
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
print (--------)
print (j)
print (--------)
print (a[j])

>>>
--------
[2 3 1 0]
--------
[1 2 3 4]

 

numpy简单用法2

标签:--   方法   copy   分享   rgs   span   nbsp   false   play   

原文地址:https://www.cnblogs.com/muziyi/p/8999435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!