推荐学习书目
› 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 · Jul 26, 2018 · 2363 views
    This topic created in 2983 days ago, the information mentioned may be changed or developed.

    ['/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 replies  •  2018-07-31 14:10:35 +08:00
    ranleng
        1
    ranleng  
       Jul 26, 2018
    filter(lambda x:not x.startswith("/HomeV2"), lst)
    compiler
        2
    compiler  
       Jul 26, 2018
    @ranleng filter 重新构造迭代结构了吧。楼主用 remove 接口试试
    KeatingSmith
        3
    KeatingSmith  
       Jul 26, 2018
    for i in range(len(source_list)):
    if source_list[I].startswith('/HomeV2'):
    source_list.remove(i)
    ranleng
        4
    ranleng  
       Jul 27, 2018
    @compiler 诶. (没认真审题..
    noqwerty
        5
    noqwerty  
       Jul 29, 2018
    [x for x in url_list if "/HomeV2" not in x]
    frmongo
        6
    frmongo  
       Jul 31, 2018
    如下

    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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2440 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 11:53 · PVG 19:53 · LAX 04:53 · JFK 07:53
    ♥ Do have faith in what you're doing.