标题: 从字符串list中删除所有空串 参看: Remove empty strings from a list of strings - [2010-10-02] https://stackoverflow.com/questions/3845423/remove-empty-strings-from-a-list-of-strings 以Python3为例。 -------------------------------------------------------------------------- x = ['a','','bb','','','ccc','','','','dddd'] while '' in x : x.remove( '' ) x最终等于: ['a', 'bb', 'ccc', 'dddd'] -------------------------------------------------------------------------- 下面是一些探讨: -------------------------------------------------------------------------- list( filter( None, x ) ) -------------------------------------------------------------------------- [i for i in x if i] -------------------------------------------------------------------------- x[:] = [i for i in x if i] 这种办法就地修改x -------------------------------------------------------------------------- ' '.join( x ).split() 这种办法有缺陷,考虑某个item中间含有空格的情形,最终结果非期望值,比如: x = ['a','','bb','','','ccc','','','','d d d d'] ' '.join( x ).split() ['a', 'bb', 'ccc', 'd', 'd', 'd', 'd'] -------------------------------------------------------------------------- [i.strip() for i in x if i.strip()] -------------------------------------------------------------------------- list( filter( lambda i:i.strip(), x ) ) -------------------------------------------------------------------------- 注意领会精神,原始需求有所变化时,做相应修改。