简体   繁体   English

从列表python中删除重复项

[英]removing duplicates from a list python

I have two lists: 我有两个清单:

list1 = [IM12345, IM12346, IM12347, IM12348]
list2 = [ID300, ID404, ID300, ID601]

list2 associates with the corresponding list1 values. list2与相应的list1值相关联。 list1 has unique values where as list2 has duplicates. list1具有唯一的值,而list2具有重复的值。

I want to make list2 unique corresponding associated value will add in the that list2 value. 我要使list2唯一对应的关联值将添加到该list2值中。

Dict= {ID300: {IM12345, IM12347}, ID404: IM12346, ID601: IM12348}

Above pattern can be in list, set or dictionary. 上面的图案可以在列表,集合或字典中。

Which algorithm in python should I use to get the above result? 我应该使用python中的哪个算法来获得以上结果?

You could try collections.defaultdict : 您可以尝试collections.defaultdict

from collections import defaultdict
d = defaultdict(set)

list1 = ['IM12345', 'IM12346', 'IM12347', 'IM12348']
list2 = ['ID300', 'ID404', 'ID300', 'ID601']

for key, value in zip(list2, list1):
    d[key].add(value)

Demo: 演示:

>>> d
defaultdict(<class 'set'>, {'ID300': {'IM12345', 'IM12347'}, 'ID404': {'IM12346'}, 'ID601': {'IM12348'}})
>>>
>>>
>>> for i, j in d.items():
...     print(i, j)
...     
... 
ID601 {'IM12348'}
ID300 {'IM12345', 'IM12347'}
ID404 {'IM12346'}

You can create a dict to save the dataset 您可以创建一个字典来保存数据集

list1 = ["IM12345", "IM12346", "IM12347", "IM12348"]
list2 = ["ID300", "ID404", "ID300", "ID601"]

dictResult=dict()
i=0
for item in list2:
    print item
    if dictResult.has_key(item):
        dictResult[item].append(list1[i])
    else:
        dictResult[item]=[list1[i]]
    i=i+1

print dictResult

Result: 结果:

{'ID404': ['IM12346'], 'ID300': ['IM12345', 'IM12347'], 'ID601': ['IM12348']}

Might not completely be a pythonic way but - here it goes: Map the input: 可能不完全是pythonic的方式,但是-可以了:映射输入:

map = dict(zip(list1, list2))

Now you can do an inverse mapping: 现在您可以进行逆映射:

inv_map = {}
for k, v in map.iteritems():
    inv_map[v] = inv_map.get(v, [])
    inv_map[v].append(k)

Result for the example above: 上面示例的结果:

>>> inv_map
{'ID404': ['IM12346'], 'ID300': ['IM12345', 'IM12347'], 'ID601': ['IM12348']}

Another way of doing it could be with list operations. 做到这一点的另一种方法可能是使用列表操作。

yourList = ["1","2","3","4","1","2"] 
newList = [] 
for f in yourList:
    if f not in newList:
       newList.append(f)

Simple solution 简单的解决方案

from collections import defaultdict

list1 = ['IM12345', 'IM12346', 'IM12347', 'IM12348']
list2 = ['ID300', 'ID404', 'ID300', 'ID601']

d = defaultdict(list)

for n in range(len(list2)):
    d[list2[n]].append(list1[n])

print d.items()

Result: 结果:

[('ID404', ['IM12346']), ('ID300', ['IM12345', 'IM12347']), ('ID601', ['IM12348'])]

Python2.7 Documentation----defaultdict Python2.7文档---- defaultdict

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM