我正在尝试删除字典中的重复值,但无法正常工作:
samples_antibiotics_with_duplicates = {'S00541-09': ['Streptomycin', 'Sulfamethoxazole', 'Trimethoprim', 'Spectinomycin', 'Streptomycin', 'Streptomycin', 'Trimethoprim']}
samples_antibiotics = {}
for key,value in samples_antibiotics_with_duplicates.iteritems():
if value not in samples_antibiotics.values():
samples_antibiotics[key] = value
print samples_antibiotics
打印:
{'S00541-09': ['Streptomycin', 'Sulfamethoxazole', 'Trimethoprim', 'Spectinomycin', 'Streptomycin', 'Streptomycin', 'Trimethoprim']}
任何帮助将不胜感激.
解决方法:
您可以尝试以下方法:
samples_antibiotics_with_duplicates = {'S00541-09': ['Streptomycin', 'Sulfamethoxazole', 'Trimethoprim', 'Spectinomycin', 'Streptomycin', 'Streptomycin', 'Trimethoprim']}
new_dict = {a:list(set(b)) for a, b in samples_antibiotics_with_duplicatates.items()}