Python 中的列表 算法问题

2018-07-26 18:58:48 +08:00
 bestehen

['/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 的长字符串

1536 次点击
所在节点    Python
6 条回复
ranleng
2018-07-26 21:03:04 +08:00
filter(lambda x:not x.startswith("/HomeV2"), lst)
compiler
2018-07-26 23:21:29 +08:00
@ranleng filter 重新构造迭代结构了吧。楼主用 remove 接口试试
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
2018-07-27 09:58:49 +08:00
@compiler 诶. (没认真审题..
noqwerty
2018-07-29 04:58:37 +08:00
[x for x in url_list if "/HomeV2" not in x]
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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/474444

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX