从列表字典中查找最小的键子集

时间:2016-05-02 13:44:45

标签: python list dictionary subset

对于一项练习,我必须确定包含所有给定蛋白质的蛋白质组的最小子集。我可以使用的对象看起来像这样:

列表字典,其中包含蛋白质组ID作为键,以及包含在其中的蛋白质的蛋白质ID列表。我还有一系列蛋白质ID。多个蛋白质组可以具有相同的蛋白质ID。

问题:找到包含阵列中声明的所有蛋白质的蛋白质组的最小子集。

可视化:

列表词典

#pragma mark - NSKeyValueObserving - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(__unused NSDictionary *)change context:(void *)context { if (context == AFTaskCountOfBytesReceivedContext) { if ([keyPath isEqualToString:NSStringFromSelector(@selector(countOfBytesReceived))]) { if ([object countOfBytesExpectedToReceive] > 0) { dispatch_async(dispatch_get_main_queue(), ^{ //You can do your stuff at here like show progress NSLog(@"Progress : %f",[object countOfBytesReceived] / ([object countOfBytesExpectedToReceive] * 1.0f)); }); } } if ([keyPath isEqualToString:NSStringFromSelector(@selector(state))]) { if ([(NSURLSessionTask *)object state] == NSURLSessionTaskStateCompleted) { @try { [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(state))]; NSLog(@"Image Download Complete"); if (context == AFTaskCountOfBytesReceivedContext) { [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(countOfBytesReceived))]; } } @catch (NSException * __unused exception) {} } } } }

阵列

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

此示例中的输出应为

['A9IWM9', 'A9IWP5','A0A0T9PBK6']

最好的问候

1 个答案:

答案 0 :(得分:0)

不使用fornd中的append和avoid方法......

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