简体   繁体   中英

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. I also have an array of protein IDs. Multiple proteomes can have the same protein IDs.

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...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM