Python-在字符串列表中查找不重复的字符
我很难在字典中找到不重复的值Python-在字符串列表中查找不重复的字符,python,dictionary,duplicates,Python,Dictionary,Duplicates,我很难在字典中找到不重复的值 my_dict = {(1, 1):'2345', (1, 2):'234', (1, 3):'5678', (1, 4):'2387'} 我需要的是能够找出哪些字符不是重复字符,哪些键是值。 对于本词典,我需要返回以下内容: >>> {(1, 3):'6'} 6是唯一没有出现在任何其他值中的值,因此需要返回键((1,3))和非重复项(6) 任何帮助都将不胜感激 my_dict = {(1, 1):'2345', (1, 2):'234',
my_dict = {(1, 1):'2345', (1, 2):'234', (1, 3):'5678', (1, 4):'2387'}
我需要的是能够找出哪些字符不是重复字符,哪些键是值。
对于本词典,我需要返回以下内容:
>>> {(1, 3):'6'}
6是唯一没有出现在任何其他值中的值,因此需要返回键((1,3))和非重复项(6)
任何帮助都将不胜感激
my_dict = {(1, 1):'2345', (1, 2):'234', (1, 3):'5678', (1, 4):'2387'}
from collections import defaultdict
d = defaultdict(list)
for k, v in my_dict.items():
for char in v:
d[char].append(k)
print {v[0]:k for k, v in d.items() if len(v) == 1}
如果你只想用字典
d = {}
for k, v in my_dict.items():
for char in v:
d.setdefault(char, []).append(k)
print {v[0]:k for k, v in d.items() if len(v) == 1}
输出
{(1, 3): '6'}
为什么说6是唯一一个在别处没有出现的值?1呢?很抱歉,输入错误,1不应该出现在(1,1)中