Python list.remove()缺陷,解决remove无法移除元素问题
例子:
with open('email.txt') as f:
email_list = f.read().split('\n')
print(email_list) # ['xiaosaxu@yeah.net', '', '']
for i in email_list:
if not i:
email_list.remove(i)
print(email_list) # ['xiaosaxu@yeah.net', '']
email_list2=[]
for i in email_list:
if '@' in i:
email_list2.append(i)
print(email_list2) # ['xiaosaxu@yeah.net']
综上: 删除列表中的元素不能用remove(),必须另外建立列表将符合的元素存进去