有1个有序日期列表A['2016-01-01','2016-01-02','2016-01-03',....'2017-06-01']
和1个无序的但是是需要排除的日期的列表B['2016-03-02','2016-03-08',]
希望把A中的包含的B元素全部去除掉,下面的写法可有不妥?
for x in B:
A.remove(x)
高洛峰2017-06-12 09:24:36
看你需求吧,没啥毛病,数据也不是很多, 我提供一种方案
from collections import OrderedDict
d_set = OrderedDict.fromkeys(A)
for x in B:
del d_set[x]
A = d_set.keys()
三叔2017-06-12 09:24:36
这种写法会报错,如果x 不在A 中就会报错,这种写法可以先加个 if 判断, x 是否在 A 中再执行 A.remove(x)
试试这个简单的写法:
#coding=utf-8
A = ['2016-01-01','2016-01-02','2016-01-03','2017-06-01','2017-06-01','2016-03-08','2016-03-08']
B = ['2016-03-02','2016-03-08']
C = []
for a in A:
for b in B:
if a == b:
break
else:
C.append(a)
print C
黄舟2017-06-12 09:24:36
from collections import OrderedDict
A = ['2016-01-01','2016-01-02','2016-01-03','2017-06-01','2016-03-08']
B = ['2016-03-02','2016-03-08']
d_set = OrderedDict.fromkeys(A)
对于 B 中的 x:
A = d_set.keys()