推荐学习书目
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
winglight2016
V2EX  ›  Python

请教一个 Python 列表解析的问题

  •  
  •   winglight2016 · Nov 21, 2017 · 3152 views
    This topic created in 3183 days ago, the information mentioned may be changed or developed.

    代码:our_predictions = probs[:,0],

    这一句里面的冒号是什么作用? probs 是个二维数组吧?

    我问了个朋友,他说这是列表解析的语法,但是他也说不清楚,请教一下高手这一句是在做什么? BTW,这是在看深度学习时候碰到的代码

    11 replies    2017-11-22 11:56:56 +08:00
    enenaaa
        1
    enenaaa  
       Nov 21, 2017
    numpy 用法。
    2 维矩阵截取所有行第 1 列。
    winglight2016
        2
    winglight2016  
    OP
       Nov 21, 2017
    @enenaaa 谢谢大佬!不过,这里没有使用 numpy 吧?应该就是 python 的语法吧
    8820670
        3
    8820670  
       Nov 21, 2017 via Android
    @winglight2016 python 列表的切片
    winglight2016
        4
    winglight2016  
    OP
       Nov 21, 2017
    @enenaaa 哦,明白了,这里不是数组,是矩阵类型
    winglight2016
        5
    winglight2016  
    OP
       Nov 21, 2017
    @8820670 我试了一下代码,列表的话,是不能这样使用的
    lrxiao
        6
    lrxiao  
       Nov 21, 2017   ❤️ 1
    233 其实这都是 python 对象。。
    相当于传了一个
    (slice(None, None, None), 2)的 tuple 给__getitem__
    但是:单独不是 py 对象 不像...
    lrxiao
        7
    lrxiao  
       Nov 21, 2017
    如果你修改__getitem__是可以用的
    winglight2016
        8
    winglight2016  
    OP
       Nov 21, 2017
    @lrxiao 对,我去看了 keras 的代码,这个 probs 对象就是 model 的 predict_generator 方法返回的 numpy 二维矩阵对象,所以支持这种方法,普通的 python 数组是不支持的
    bigeast
        9
    bigeast  
       Nov 21, 2017
    用过 Matlab 的同学就不会有这个疑问😂
    siteshen
        10
    siteshen  
       Nov 21, 2017
    楼上虽然说明了有些内容会转成 tuple,不过以前没见过这种写法,还是不清楚是怎么转成 tuple 的,就研究了下。

    https://docs.python.org/3/reference/expressions.html#slicings

    The semantics for a slicing are as follows. The primary is indexed (using the same __getitem__() method as normal subscription) with a key that is constructed from the slice list, as follows. If the slice list contains at least one comma, the key is a tuple containing the conversion of the slice items; otherwise, the conversion of the lone slice item is the key. The conversion of a slice item that is an expression is that expression. The conversion of a proper slice is a slice object (see section The standard type hierarchy) whose start, stop and step attributes are the values of the expressions given as lower bound, upper bound and stride, respectively, substituting None for missing expressions.

    对于表达式 a[`.+`] 如果 正则 `.+` 至少包含一个逗号,则 key 为一个 tuple,否则 key 为 slice。
    下面的示例能更清楚地说明上面的规则:

    $ cat test.py && python3 test.py
    class A():
    def __getitem__(self, *args, **kwargs):
    print('args:', len(args), args[0])

    a = A()
    a[1:]
    a[:2]
    a[1:2]
    a[1,:]
    a[:,2]
    a[1,:,2]
    # end of test.py

    args: 1 slice(1, None, None)
    args: 1 slice(None, 2, None)
    args: 1 slice(1, 2, None)
    args: 1 (1, slice(None, None, None))
    args: 1 (slice(None, None, None), 2)
    args: 1 (1, slice(None, None, None), 2)
    winglight2016
        11
    winglight2016  
    OP
       Nov 22, 2017
    @siteshen 感谢大佬!这种用法居然是当做一个 tuple 参数了,终于理解了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   874 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 22:05 · PVG 06:05 · LAX 15:05 · JFK 18:05
    ♥ Do have faith in what you're doing.