当列表中存在较多的空字符时, 可用的处理方法.
Python - 去除list中的空字符

method1:

while '' in index:
    index.remove('')

method2:

Python内建filter()函数 - 过滤list
filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素

def not_empty(s):
    return s and s.strip()

res = filter(not_empty, ['1', '', '2', None, '3', '  '])
# 结果: ['1', '2', '3']
Last modification:October 9, 2018
If you think my article is useful to you, please feel free to appreciate