我有一个列表字典,我需要通过删除列表中的元素来从列表中选择唯一元素,如果它重复第2次,第3次等等,并且仅在第一次保留时保留。我的数据:
dicti={'a':['[email protected]'.,'nr@context'],
'b':['[email protected]','nr@id'],
'c':['nr@context''[email protected]']}
我试过的代码
checker=list()
for key in emails:
for emailid in emails[key]:
if emailid in checker:
del(emailid)
else:
checker.append(emailid)
保持原始代码的精神:
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
而不是删除为什么不从数据中获取所需内容?
dicti={'a':['[email protected]','nr@context'],
'b':['[email protected]','nr@id'],
'c':['nr@context','[email protected]']}
track=[]
for key,value in dicti.items():
for item in value:
if item not in track:
track.append(item)
print(track)
输出:
['[email protected]', 'nr@id', 'nr@context', '[email protected]']