遇到一个列表生成器的句子。不知道怎么实现了。 - V2EX
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 3420 次点击
    这是一个创建于 2737 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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     Solana     822 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 19:55 PVG 03:55 LAX 11:55 JFK 14:55
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86