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

请教一小段 python 程序

  •  
  •   wffett · 2015-07-27 18:50:10 +08:00 · 2227 次点击
    这是一个创建于 3197 天前的主题,其中的信息可能已经有所发展或是发生改变。

    新手,基础差

    当z2为False时,输出z1对应的位置上的值

    z1=[1,2,3,4,5,6,7,8,9]
    z2=[True,False,False,True,True,True,True,True,False]
    z1=z1[~x for x in z2] #这句有问题
    print z1

    因为我看到一段程序,是这样表达的:
    x=x[~sp.isnan(y)]
    y=y[~sp.isnan(y)]
    不知道有什么不同

    6 条回复    2015-07-27 22:20:14 +08:00
    cc7756789
        1
    cc7756789  
       2015-07-27 19:05:14 +08:00
    >>> for x in zip(z1, z2):
    ... if not x[1]:
    ... print x[0]
    ...
    2
    3
    9

    >>> n = 0
    >>> for x in z2:
    ... if not x:
    ... print z1[n]
    ... n += 1
    ...
    2
    3
    9
    mianju
        2
    mianju  
       2015-07-27 20:33:22 +08:00
    ```
    for i in range(len(z1)):
    if not z2[i]:
    print z1[i]
    ```
    应该也可以
    mianju
        3
    mianju  
       2015-07-27 20:33:56 +08:00
    缩进没弄好,抱歉就将就看看吧> <
    scream7
        4
    scream7  
       2015-07-27 20:50:49 +08:00
    z1 = [x for x,y in zip(z1,z2) if y]
    wffett
        5
    wffett  
    OP
       2015-07-27 21:02:39 +08:00
    @cc7756789
    @mianju
    @scream7
    谢谢以上各位,从你们的答案中我学到了很多额外的收获。
    这个问题是我搞错了数据类型,我以为sp.isnan(y)返回的列表,其实是数组,所以对应xy的类型也应该是数组。因此我的问题就好解决了

    import scipy as sp

    x=sp.array([1,2,3,4,5,6,7,8,9])
    y=sp.array([True,False,True,True,True,False,True,False,True])

    x=x[y]

    print(x)
    BooksE
        6
    BooksE  
       2015-07-27 22:20:14 +08:00
    lz是从R转过来的么..
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5403 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:14 · PVG 16:14 · LAX 01:14 · JFK 04:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.