gpt4 book ai didi

python - 删除具有作为另一个键的子集的值的键的 Python 字典条目

转载 作者:行者123 更新时间:2023-12-01 04:36:52 25 4
gpt4 key购买 nike

我有一个使用 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

最佳答案

你可以试试这个

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']}

关于python - 删除具有作为另一个键的子集的值的键的 Python 字典条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31561006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com