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

Python 中的列表 算法问题

  •  
  •   bestehen · 2018-07-26 18:58:48 +08:00 · 1529 次点击
    这是一个创建于 2093 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ['/N/98445.html', '/N/98439.html', '/HomeV2/Banner/Records?model=A1&name=A1&url=https%3A%2F%2Fwww.jiemodui.com%2FKe%2F97539.html%3Ffrom%3Djmd004&flag=3a6f8321cf5ca56b13fd4a40a9474246', '/N/98441.html', '/N/98436.html', '/N/98435.html', '/N/98433.html', '/N/98434.html', '/HomeV2/Banner/Records?model=A3&name=A3&url=https%3A%2F%2Fwww.jiemodui.com%2FA%2F98137%3F%3Dxxl3&flag=90e994d56cd13c6b3201012bc988873a', '/N/98432.html', '/N/98431.html', '/N/98430.html', '/N/98429.html', '/N/98427.html', '/N/98426.html', '/N/98421.html', '/N/98416.html', '/N/98420.html', '/N/98407.html', '/N/98419.html', '/N/98411.html', '/N/98406.html']

    这里我想保留 /N/98445.html 类似的格式,把 很长的 /HomeV2 那两个很长的从列表中删除,我想到的办法就是遇到这个字符串 pass 然后 重新构造一个列表添加,因为可能这个带 /HomeV2 字符串不在这个位置 有没有不需要重新构造一个列表直接在原来的列表删除这两个有 /HomeV2 的长字符串

    6 条回复    2018-07-31 14:10:35 +08:00
    ranleng
        1
    ranleng  
       2018-07-26 21:03:04 +08:00
    filter(lambda x:not x.startswith("/HomeV2"), lst)
    compiler
        2
    compiler  
       2018-07-26 23:21:29 +08:00
    @ranleng filter 重新构造迭代结构了吧。楼主用 remove 接口试试
    KeatingSmith
        3
    KeatingSmith  
       2018-07-26 23:27:17 +08:00
    for i in range(len(source_list)):
    if source_list[I].startswith('/HomeV2'):
    source_list.remove(i)
    ranleng
        4
    ranleng  
       2018-07-27 09:58:49 +08:00
    @compiler 诶. (没认真审题..
    noqwerty
        5
    noqwerty  
       2018-07-29 04:58:37 +08:00
    [x for x in url_list if "/HomeV2" not in x]
    frmongo
        6
    frmongo  
       2018-07-31 14:10:35 +08:00
    如下

    a= [.......上面那个 list]
    for ele in a:
    if ele.startwith("/HomeV2") or len(ele)>13:
    a.remove(ele)
    print "remove elements from a [%s]" % ele
    else:
    pass
    print a
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1573 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 17:01 · PVG 01:01 · LAX 10:01 · JFK 13:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.