Differences between revisions 2 and 3
Revision 2 as of 2009-10-16 08:36:21
Size: 1947
Editor: limodou
Comment:
Revision 3 as of 2009-12-25 07:10:32
Size: 1947
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
[[TableOfContents]] <<TableOfContents>>
Line 6: Line 6:
[[Include(ZPyUGnav)]] <<Include(ZPyUGnav)>>

秒杀 list 中的空item

filter(lambda)

Heroboy <yangweiqin@gmail.com>
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 <smallfish@live.cn>
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 <khsing.cn@gmail.com>
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 <lanphaday@gmail.com>
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 <yangweiqin@gmail.com>
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 <lqs.buaa@gmail.com>
reply-to        python-cn@googlegroups.com
to      python-cn`CPyUG`华蟒用户组 <python-cn@googlegroups.com>
date    Fri, Jan 9, 2009 at 17:06
subject [CPyUG:76495] Re: 怎么用快速把一个list中的空item去除?

a = filter(None, a)


反馈

MiscItems/2009-01-16 (last edited 2009-12-25 07:10:32 by localhost)