[英]Finding a minimal subset of keys from a dictionary of lists
For an exercise I have to determine a smallest subset of proteomes that contain all given proteins. 为了进行练习,我必须确定包含所有给定蛋白质的最小蛋白质组。 The objects I can work with look like that: 我可以使用的对象如下所示:
A dictionary of lists which has the proteome ID as keys and a list of protein IDs of proteins that are contained in it. 以蛋白质组ID为键的列表字典,以及其中包含的蛋白质的蛋白质ID列表。 I also have an array of protein IDs. 我也有一系列蛋白质ID。 Multiple proteomes can have the same protein IDs. 多个蛋白质组可以具有相同的蛋白质ID。
Question : Find the smallest subset of proteomes containing all proteins declared in the array. 问题 :查找包含蛋白质组中声明的所有蛋白质的最小蛋白质组。
Visualisation: 可视化:
Dictionary of lists 列表字典
{'UP000040088': ['A0A0T9TGA2', 'A0A0T9PBK6'],'UP000005347': ['I2WKK5', 'I2W7Q9', 'I2WH23', 'I2W8G3', 'I2W8S8', 'I2WCH8', 'I2WCJ2', 'I2WA21', 'I2WC26', 'I2WCG9', 'I2W9F2', 'I2WKG5', 'I2W4G7', 'I2WCD6', 'I2WG92', 'I2W6I6', 'I2W648', 'I2WE51', 'I2WKU2', 'I2WIG4', 'I2WED9', 'I2WEM0', 'I2WB05', 'I2W998', 'I2W7Q9', 'I2WA37', 'I2WD89', 'I2WEB4', 'I2W4G7', 'I2W4B1', 'I2WIM9', 'I2WI84', 'I2WIS6', 'I2WES7', 'I2WGL9', 'I2WIA8', 'I2W7H0', 'I2WDB3', 'I2WE60', 'I2WC93', 'I2WC36', 'I2WC86', 'I2WC82', 'I2W6J9', 'I2W428', 'I2WCH8', 'I2WCJ2', 'I2W9T1', 'I2W9B9', 'I2WC26', 'I2WCG9', 'I2WA28', 'I2WA21', 'I2W648', 'I2WE51', 'I2WKU2', 'I2WIG4', 'I2WEM0', 'I2WED9', 'I2W9F2'], 'UP000001592': ['A9IMD2', 'A9IU64', 'A9IWM9', 'A9IWP5', 'A9IZ28', 'A9IZ30', 'A9IZ48', 'A9IZ71', 'A9IZ73', 'A9IZ75']}
Array 数组
['A9IWM9', 'A9IWP5','A0A0T9PBK6']
The output in this example should be 此示例中的输出应为
'UP000040088':['A0A0T9PBK6'],'UP000001592':['A9IWM9', 'A9IWP5']
best regards 最好的祝福
Without using append and avoiding methods in for loops... 不使用添加和避免for循环中的方法...
dct={'UP000040088': ['A0A0T9TGA2', 'A0A0T9PBK6'],'UP000005347': ['I2WKK5', 'I2W7Q9', 'I2WH23', 'I2W8G3', 'I2W8S8', 'I2WCH8', 'I2WCJ2', 'I2WA21', 'I2WC26', 'I2WCG9', 'I2W9F2', 'I2WKG5', 'I2W4G7', 'I2WCD6', 'I2WG92', 'I2W6I6', 'I2W648', 'I2WE51', 'I2WKU2', 'I2WIG4', 'I2WED9', 'I2WEM0', 'I2WB05', 'I2W998', 'I2W7Q9', 'I2WA37', 'I2WD89', 'I2WEB4', 'I2W4G7', 'I2W4B1', 'I2WIM9', 'I2WI84', 'I2WIS6', 'I2WES7', 'I2WGL9', 'I2WIA8', 'I2W7H0', 'I2WDB3', 'I2WE60', 'I2WC93', 'I2WC36', 'I2WC86', 'I2WC82', 'I2W6J9', 'I2W428', 'I2WCH8', 'I2WCJ2', 'I2W9T1', 'I2W9B9', 'I2WC26', 'I2WCG9', 'I2WA28', 'I2WA21', 'I2W648', 'I2WE51', 'I2WKU2', 'I2WIG4', 'I2WEM0', 'I2WED9', 'I2W9F2'], 'UP000001592': ['A9IMD2', 'A9IU64', 'A9IWM9', 'A9IWP5', 'A9IZ28', 'A9IZ30', 'A9IZ48', 'A9IZ71', 'A9IZ73', 'A9IZ75']}
a = ['A9IWM9', 'A9IWP5','A0A0T9PBK6']
match={k:[None for _ in a] for k in dct.keys()}
for k,lst in dct.items():
n=0
for ai in a:
if ai in lst:
match[k][n]=ai
n+=1
print match # {'UP000040088': [None, None, 'A0A0T9PBK6'], 'UP000005347': [None, None, None], 'UP000001592': ['A9IWM9', 'A9IWP5', None]}
match={k:[vi for vi in v if not vi is None] for k,v in match.items()}
print match # {'UP000040088': ['A0A0T9PBK6'], 'UP000005347': [], 'UP000001592': ['A9IWM9', 'A9IWP5']}
match={k:v for k,v in match.items() if len(v)}
print match # {'UP000040088': ['A0A0T9PBK6'], 'UP000001592': ['A9IWM9', 'A9IWP5']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.