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
LeIYc
V2EX  ›  Python

遇到一个列表生成器的句子。不知道怎么实现了。

  •  
  •   LeIYc · 2018-07-06 14:35:18 +08:00 · 2762 次点击
    这是一个创建于 2115 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Y1 = [[int(x0 + x1 <1) for (x0,x1) in X ]]

    print("Y1",Y1)

    Y2 = []

    for x0,x1 in X:

    if x0 + x1 <1:
    
    
        Y2.append(1)
        
        
    else:
    
    
        Y2.append(0)
    

    print('Y2',Y2)

    我用简单方法写出来了。但是还是想理解下 Y1 的生成逻辑。 或者有没有相关的教程。

    11 条回复    2018-07-06 17:51:21 +08:00
    cosven
        1
    cosven  
       2018-07-06 14:38:49 +08:00
    http://www.pythonforbeginners.com/basics/list-comprehensions-in-python

    楼主是要个介绍 list comprehension 的教程吗 ~
    hjq98765
        2
    hjq98765  
       2018-07-06 15:00:53 +08:00
    [1 if x0+x1<1 else 0 for x0,x1 in X]
    BingoXuan
        3
    BingoXuan  
       2018-07-06 15:07:42 +08:00 via Android
    x0+x1<1,如果是真转换成整型就是 1 否则是 0,然后列表推导式遍历所有。
    LeIYc
        4
    LeIYc  
    OP
       2018-07-06 15:10:04 +08:00
    @cosven 谢谢。
    在网上找了下,没找到。
    可是这个 int () 是什么鬼。为什么会产生和[1 if x0+x1<1 else 0 for x0,x1 in X] 同样的效果。
    LeIYc
        5
    LeIYc  
    OP
       2018-07-06 15:13:42 +08:00
    @BingoXuan 也就是说,int () 就是把 x0 + x1 的结果值进行了一下转换。
    这样啊。OK 懂了。有没有这类的教程可以学习学习啊。
    Sylv
        6
    Sylv  
       2018-07-06 15:17:07 +08:00 via iPhone
    @LeIYc
    int() 就是把数据类型转换为整型数字。
    在很多语言里,True 的内部值就是 1,False 是 0。
    >>> True == 1
    True
    >>> False == 0
    True
    Hiyagg
        7
    Hiyagg  
       2018-07-06 15:17:44 +08:00
    @LeIYc int(Ture) = 1 , int(False) = 0
    BingoXuan
        8
    BingoXuan  
       2018-07-06 15:34:43 +08:00 via Android
    @LeIYc 廖雪峰的教程我觉得已经很全面了,但我不建议过多使用 lambda 或者推导式,会让代码看起来很迷惑。你这个算是比较巧妙处理这个问题。再复杂一点就开始恶心了。最好就拆开多个推导式处理
    xpresslink
        9
    xpresslink  
       2018-07-06 15:56:19 +08:00
    Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> issubclass(bool, int)
    True
    >>> isinstance(True, int)
    True
    >>> True.numerator
    1
    >>> False.numerator
    0
    >>>
    LeIYc
        10
    LeIYc  
    OP
       2018-07-06 16:28:08 +08:00
    @BingoXuan 我是在 B 站上看 tensorflow 的视频,有一个北大的讲这个。然后里面就用了这个列表生成式。
    当时谢了写没运行成功。而且在列表生成式中的 if and or 这个规律逻辑一直就不怎么懂。
    然后就卡在这了。
    另外,想请教各位 V 友们。怎么学好 tensorflow 和入门深度学习。
    gnozix
        11
    gnozix  
       2018-07-06 17:51:21 +08:00
    这两个得出来的结果不一样啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2644 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:35 · PVG 19:35 · LAX 04:35 · JFK 07:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.