去除list中空字符串的方法

在Python的list列表中,有时需要去除空字符串,有以下两种方法:

  1. filter

  2. 列表生成式

  1. 用 filter 最快
1
filter(None, ulist)

也可以使用生成式

1
ulist = [x for x in ulist if x != '']

或者

1
2
while '' in ulist:
ulist.remove('')

Comments

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×