推荐学习书目
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 · Oct 29, 2015 · 4347 views
    This topic created in 3851 days ago, the information mentioned may be changed or developed.

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

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

    请教,谢谢

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

    T = (1, 2, 3)

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

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

    代替 pass
    thinker3
        6
    thinker3  
       Oct 29, 2015   ❤️ 1
    >>> a[...] = 'a'
    >>> a
    {Ellipsis: 'a'}
    northisland
        7
    northisland  
    OP
       Oct 29, 2015
    发现了第二个例子的一点点用法
    ( 这不是 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  
       Oct 29, 2015
    http://blog.brush.co.nz/2009/05/ellipsis/ 这里好像有个然并卵的例子
    cszhiyue
        9
    cszhiyue  
       Oct 30, 2015
    @northisland 错别字。。维度 相当于
    master13
        10
    master13  
       Oct 30, 2015
    我与大数据不共戴天!
    northisland
        11
    northisland  
    OP
       Sep 28, 2017
    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 自定义的“黑话”
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   891 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 20:51 · PVG 04:51 · LAX 13:51 · JFK 16:51
    ♥ Do have faith in what you're doing.