我正在尝试在字典值中包含唯一列表。我已经实现了使用此Uniqueness for list of lists
我的疑问是如何将数据写回到字典中。到目前为止,这是我所做的:
final_dict = {'SELECT: AutoSelect DF2F': [['D80', 'FoldersDF2FAutoSelect'], ['DF90', 'FoldersDF2FAutoSelect'], ['DF80', 'FoldersDF2FAutoSelect'], ['DF90', 'PB:FoldersDF2FAutoSelect']], 'SELECT: TB70001': [['DF80', 'FolderSelected'], ['DF90', 'PB:FolderSelected'], ['DF80', 'FolderSelected'], ['DF90', 'FolderSelected']]}
for v in final_dict.values():
unique_data = [list(x) for x in set(tuple(x) for x in v)]
如果您看我的词典有这么多重复的列表,则是一个值。我消除了重复项。现在,我如何将其转换回字典?
答案 0 :(得分:2)
使用简单的迭代
例如:
final_dict = {'SELECT: AutoSelect DF2F': [['D80', 'FoldersDF2FAutoSelect'], ['DF90', 'FoldersDF2FAutoSelect'], ['DF80', 'FoldersDF2FAutoSelect'], ['DF90', 'PB:FoldersDF2FAutoSelect']], 'SELECT: TB70001': [['DF80', 'FolderSelected'], ['DF90', 'PB:FolderSelected'], ['DF80', 'FolderSelected'], ['DF90', 'FolderSelected']]}
for k, v in final_dict.items():
final_dict[k] = [list(x) for x in set(tuple(x) for x in v)]
pprint(final_dict)
输出:
{'SELECT: AutoSelect DF2F': [['DF90', 'FoldersDF2FAutoSelect'],
['D80', 'FoldersDF2FAutoSelect'],
['DF90', 'PB:FoldersDF2FAutoSelect'],
['DF80', 'FoldersDF2FAutoSelect']],
'SELECT: TB70001': [['DF90', 'PB:FolderSelected'],
['DF80', 'FolderSelected'],
['DF90', 'FolderSelected']]}
答案 1 :(得分:0)
您快到了,在for循环中,跟踪键k
,并将unique_data
保存在该键处的字典中:
final_dict = {'SELECT: AutoSelect DF2F': [['D80', 'FoldersDF2FAutoSelect'], ['DF90', 'FoldersDF2FAutoSelect'], ['DF80', 'FoldersDF2FAutoSelect'], ['DF90', 'PB:FoldersDF2FAutoSelect']], 'SELECT: TB70001': [['DF80', 'FolderSelected'], ['DF90', 'PB:FolderSelected'], ['DF80', 'FolderSelected'], ['DF90', 'FolderSelected']]}
for k,v in final_dict:
unique_data = [list(x) for x in set(tuple(x) for x in v)]
final_dict[k] = unique_data