##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = 秒杀 list 中的空item = ##startInc == filter(lambda) == {{{ Heroboy reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Fri, Jan 9, 2009 at 15:30 subject [CPyUG:76474] Re: 怎么用快速把一个list中的空item去除? }}} `aa=filter(lambda x:x=='',aa);` == 列表推导 == {{{ smallfish reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Fri, Jan 9, 2009 at 15:31 subject [CPyUG:76475] Re: 怎么用快速把一个list中的空item去除? }}} {{{ 1. aa = [for i in aa if i != ''] 2. bb = set(aa) cc = [for i in bb if i != ''] }}} {{{ khsing reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Fri, Jan 9, 2009 at 18:20 }}} `[i for i in list if i]` {{{ LaiYonghao reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Fri, Jan 9, 2009 at 18:48 }}} 突然想起,楼主的空行的意思估计是指由空白符组成的行。 那就得用 `lines = [line for line in f if not line.isspace()]` == itertools.ifiter() == {{{ Heroboy reply-to python-cn@googlegroups.com to python-cn@googlegroups.com date Fri, Jan 9, 2009 at 16:45 subject [CPyUG:76494] Re: 怎么用快速把一个list中的空item去除? }}} {{{ for i in itertools.ifiter(f,lambda x:x): pass }}} == filter() == {{{ Liu Qishuai reply-to python-cn@googlegroups.com to python-cn`CPyUG`华蟒用户组 date Fri, Jan 9, 2009 at 17:06 subject [CPyUG:76495] Re: 怎么用快速把一个list中的空item去除? }}} `a = filter(None, a)` ##endInc ---- '''反馈'''