我使用defaultdict
生成了字典:
{"GGGAAATTTCCCTTTGGGAAACGG": ["9/1", "9/2", "1/1.1", "9/2.1"],
"GGGAAATTTCCCTTTGGGAAAGCC": ["9/2", "9/2.1"],
"GGGAAATTTCCCTTTGGGAAAGGG": ["1/1", "1/2", "9/1", "1/1.1"]}
其中一个企业在其价值方面是另一个企业的子集:
"GGGAAATTTCCCTTTGGGAAAGCC": ["9/2", "9/2.1"]
是
的子集"GGGAAATTTCCCTTTGGGAAACGG": ["9/1", "9/2", "1/1.1", "9/2.1"]
如何折叠字典以便最终获得这些结果?
{"GGGAAATTTCCCTTTGGGAAACGG": ["9/1", "9/2", "1/1.1", "9/2.1"],
"GGGAAATTTCCCTTTGGGAAAGGG": ["1/1", "1/2", "9/1", "1/1.1"]}
或
{["GGGAAATTTCCCTTTGGGAAACGG", "GGGAAATTTCCCTTTGGGAAAGCC"]:
["9/1", "9/2", "1/1.1", "9/2.1"],
"GGGAAATTTCCCTTTGGGAAAGGG":
["1/1", "1/2", "9/1", "1/1.1"]}
编辑:
根据要求,这是我的尝试:
#dd is my defaultdict
for keys, values in dd.iteritems():
if all(for item in values:
if item in dd.items():
return True
else:
return False):
print keys
答案 0 :(得分:2)
你可以试试这个
mydict = {"GGGAAATTTCCCTTTGGGAAACGG": ["9/1", "9/2", "1/1.1", "9/2.1"],
"GGGAAATTTCCCTTTGGGAAAGCC": ["9/2", "9/2.1"],
"GGGAAATTTCCCTTTGGGAAAGGG": ["1/1", "1/2", "9/1", "1/1.1"]}
>>>dict([i for i in mydict.items() if not any(set(j).issuperset(set(i[1])) and j!=i[1] for j in mydict.values())])
{'GGGAAATTTCCCTTTGGGAAACGG': ['9/1', '9/2', '1/1.1', '9/2.1'],
'GGGAAATTTCCCTTTGGGAAAGGG': ['1/1', '1/2', '9/1', '1/1.1']}
或者只是
for i in mydict.items():
for j in mydict.values():
if i[1]!=j:
if set(j).issuperset(set(i[1])):
mydict.pop(i[0])
>>>mydict
{'GGGAAATTTCCCTTTGGGAAACGG': ['9/1', '9/2', '1/1.1', '9/2.1'],
'GGGAAATTTCCCTTTGGGAAAGGG': ['1/1', '1/2', '9/1', '1/1.1']}