学 python 时间不短不长,但这两句用的两个操作符还是不太清楚

2015-10-29 18:09:17 +08:00
 northisland

top[top_ind].reshape(*(blob.shape))
这一句的这个星星,是要干嘛?

top[top_ind].data[...] = blob.astype(np.float32, copy=False)
这一句的三个点,好熟悉的感觉,但不知到要干嘛

请教,谢谢

3859 次点击
所在节点    Python
11 条回复
northisland
2015-10-29 18:16:23 +08:00
>>> import numpy as np
>>> a = np.zeros( shape=(2, 3), dtype=np.float32 )
>>> a
array([[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
>>> a.shape
(2, 3)
>>> b = np.zeros( shape=(1, 6), dtype=np.float32 )
>>> b.reshape( a.shape )
array([[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)
>>> b.reshape( *(a.shape) )
array([[ 0., 0., 0.],
[ 0., 0., 0.]], dtype=float32)

*(a.shape)好像没作用。。。
icedx
2015-10-29 18:28:36 +08:00
*(a.shape) 是说把接收到的元组 a.shape 解包传给 top[top_ind].reshape()
aheadlead
2015-10-29 18:35:43 +08:00
def func(a, b, c):
print a, b, c

T = (1, 2, 3)

下面两个效果是一样的
func(1, 2, 3)
func(*T)
northisland
2015-10-29 18:52:02 +08:00
@icedx @aheadlead
谢谢,你们解答了我第一个问题
Kisesy
2015-10-29 19:28:20 +08:00
三个点,就是省略,在代码中不能写的,只有输出时才这样显示

不过用在 py3 的某些地方,例如:
def function():
...

代替 pass
thinker3
2015-10-29 20:11:46 +08:00
>>> a[...] = 'a'
>>> a
{Ellipsis: 'a'}
northisland
2015-10-29 21:43:17 +08:00
发现了第二个例子的一点点用法
( 这不是 python 通用的规则,是万恶的 numpy 库定义的“黑话” )

>>> x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x = x.reshape( (2, 5) )
定义一个 2x5 的矩阵
>>> x.reshape( (2, 5) )
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])

>>> x[..., 4]
这就相当与取矩阵第二唯独( x )的第 4 顺序的向量, so
array([4, 9])

>>> x[1, ...]
array([5, 6, 7, 8, 9])

也是一样,跟:用法差不多
>>> x[1, :]
array([5, 6, 7, 8, 9])
>>> x[:, 1]
array([1, 6])

表面测试而已
wynemo
2015-10-29 22:11:53 +08:00
http://blog.brush.co.nz/2009/05/ellipsis/ 这里好像有个然并卵的例子
cszhiyue
2015-10-30 08:22:54 +08:00
@northisland 错别字。。维度 相当于
master13
2015-10-30 09:15:13 +08:00
我与大数据不共戴天!
northisland
2017-09-28 18:03:46 +08:00
import numpy as np
a = np.array([[8, 9], [6, 4]])
b = np.zeros((2, 2), dtype=a.dtype)
b[:] = a[:]
c = np.zeros((2, 2), dtype=a.dtype)
c[:] = a
d = np.zeros((2, 2), dtype=a.dtype)
d[...] = a
e = np.zeros((2, 2), dtype=a.dtype)
e = a

其中,b, c, d 的赋值都是等效的
e 是直接引用


所以咯,...是 numpy 自定义的“黑话”

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/232079

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX