V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
northisland
V2EX  ›  Python

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

  •  
  •   northisland · 2015-10-29 18:09:17 +08:00 · 3851 次点击
    这是一个创建于 3073 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    请教,谢谢

    11 条回复    2017-09-28 18:03:46 +08:00
    northisland
        1
    northisland  
    OP
       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
        2
    icedx  
       2015-10-29 18:28:36 +08:00 via Android   ❤️ 4
    *(a.shape) 是说把接收到的元组 a.shape 解包传给 top[top_ind].reshape()
    aheadlead
        3
    aheadlead  
       2015-10-29 18:35:43 +08:00   ❤️ 4
    def func(a, b, c):
    print a, b, c

    T = (1, 2, 3)

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

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

    代替 pass
    thinker3
        6
    thinker3  
       2015-10-29 20:11:46 +08:00   ❤️ 1
    >>> a[...] = 'a'
    >>> a
    {Ellipsis: 'a'}
    northisland
        7
    northisland  
    OP
       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
        8
    wynemo  
       2015-10-29 22:11:53 +08:00
    http://blog.brush.co.nz/2009/05/ellipsis/ 这里好像有个然并卵的例子
    cszhiyue
        9
    cszhiyue  
       2015-10-30 08:22:54 +08:00
    @northisland 错别字。。维度 相当于
    master13
        10
    master13  
       2015-10-30 09:15:13 +08:00
    我与大数据不共戴天!
    northisland
        11
    northisland  
    OP
       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 自定义的“黑话”
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2975 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:32 · PVG 08:32 · LAX 17:32 · JFK 20:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.