保留两个列表之间的差异顺序 [英] Preserving the order in difference between two lists
本文介绍了保留两个列表之间的差异顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个列表l
和l_match
. l_match
是一个空列表.
I have two lists l
and l_match
. l_match
is an empty list.
l = ['gtttaattgagttgtcatatgttaataacg',
'tttaattgagttgtcatatgttaataacgg',
'ttaattgagttgtcatatgttaataacggt',
'taattgagttgtcatatgttaataacggta',
'aattgagttgtcatatgttaataacggtat']
l_match = []
print list(set(l) - set(l_match))
提供输出
['aattgagttgtcatatgttaataacggtat',
'tttaattgagttgtcatatgttaataacgg',
'ttaattgagttgtcatatgttaataacggt',
'taattgagttgtcatatgttaataacggta',
'gtttaattgagttgtcatatgttaataacg']
我希望输出与输入的顺序相同.即在上述情况下的输出 应该是
I want the output the same order as the input. i.e. in the above case the output should be
['gtttaattgagttgtcatatgttaataacg',
'tttaattgagttgtcatatgttaataacgg',
'ttaattgagttgtcatatgttaataacggt',
'taattgagttgtcatatgttaataacggta',
'aattgagttgtcatatgttaataacggtat']
您可以建议修改吗?
推荐答案
只需将l_match
设置为一组:
l_match = []
st = set(l_match)
print([ele for ele in l if ele not in st])
如果l
可能有重复,请使用OrderedDict从l
获取唯一值:
If l
can have dupes use an OrderedDict to get unique values from l
:
from collections import OrderedDict
print([ele for ele in OrderedDict.fromkeys(l) if ele not in st])
很显然,l_match
将包含现实世界中的值,或者简单的l[:] = OrderedDict.fromkeys(l)
就足以从l
中删除重复项并保持顺序
Obviously l_match
would contain values in the real world or a simple l[:] = OrderedDict.fromkeys(l)
would suffice to remove dupes from l
and keep the order
这篇关于保留两个列表之间的差异顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文