我提供以下示例。我有两个列表,一个列表,一个带有处理过的元素。如示例所示,该想法是消除已经处理的元素。问题在于,使用真实数据需要13分钟!当然可以通过某种方式对其进行优化。请帮忙。
processed = ['a','b','c']
total = [(1, 'a', 'alaksdl'),
(2, 'x', 'asda'),
(3, 'b', 'asda'),
(4, 'c', 'wef'),
(5, 'e', 'asaaa'),
(5, 'j', 'asd')
]
start = time.time()
result = [x for x in total if x[1] not in processed]
end = time.time()
print("Time: ",round(end - start,5), " segundos")
Time: 0.00014 segundos
[(2, 'x', 'asda'), (5, 'e', 'asaaa'), (5, 'j', 'asd')]
result
s = set(processed)
list(filter(lambda x: x[1] not in s, total))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句