简体   繁体   English

如何从列表字典中选择唯一元素

[英]how to select unique elements from a dictionary of lists

I had a dictionary of lists and I need to select unique elements out of all lists by dropping elements in lists if it repeats for the 2nd,3rd and so on.. times and preserving only for the first time it comes. 我有一个列表字典,如果它重复第二,第三次等等,我需要通过从列表中删除元素来从所有列表中选择唯一元素,等等。并且仅在第一次出现时保存。 my data: 我的数据:

dicti={'a':['video@vermont.org'.,'nr@context'],
'b':['vermont@vermont.org','nr@id'],
'c':['nr@context''vermont@vermont.org']}

my code which I tried 我尝试过的代码

checker=list()
for key in emails:
    for emailid in emails[key]:
        if emailid in checker:
            del(emailid)
        else:
            checker.append(emailid)

Keeping the spirit of the original code: 保留原始代码的精神:

checker=set()
for key in emails:
    value = list()
    for emailid in emails[key]:
       if emailid not in checker:
           checker.add(emailid)
           value.append(emailid)
    emails[key] = value

Instead of deleting why don't you fetch what you need from data ? 而不是删除为什么不从数据中获取所需的内容呢?

dicti={'a':['video@vermont.org','nr@context'],
'b':['vermont@vermont.org','nr@id'],
'c':['nr@context','vermont@vermont.org']}

track=[]
for key,value in dicti.items():
    for item in value:
        if item not in track:
            track.append(item)
print(track)

output: 输出:

['vermont@vermont.org', 'nr@id', 'nr@context', 'video@vermont.org']

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

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