创建将 listA 中的项目映射到 ListB 中的项目的字典很容易。如果您使用的是 Python >= 2.7,则可以使用字典理解(如果没有,请参阅其他dict
用于实现相同目的的答案):
listA = [ 3, 4, 5, 7]
ListB = ['Z2 O95409 V595 **p.His3Leu** Dase - Hophaly type 5 (HPE5) [609637]', 'Z2 O95409 V595 **p.His4Leu** Dase - Hophaly type 5 (HPE5) [609637]', 'Z2 O95409 V595 **p.His5Leu** Dase - Hophaly type 5 (HPE5) [609637]', 'Z2 O95409 V595 **p.His7Leu** Dase - Hophaly type 5 (HPE5) [609637]' ]
d = { a:b for a, b in zip(listA, ListB) }
from pprint import pprint
pprint(d)
{3: 'Z2 O95409 V595 **p.His3Leu** Dase - Hophaly type 5 (HPE5) [609637]',
4: 'Z2 O95409 V595 **p.His4Leu** Dase - Hophaly type 5 (HPE5) [609637]',
5: 'Z2 O95409 V595 **p.His5Leu** Dase - Hophaly type 5 (HPE5) [609637]',
7: 'Z2 O95409 V595 **p.His7Leu** Dase - Hophaly type 5 (HPE5) [609637]'}
但是,考虑到您的要求,我建议使用字典词典来查找蛋白质名称及其完整描述。
d = { a: {'protein': b.split('**')[1], 'description': b} for a, b in zip(listA, ListB) }
pprint(d)
{3: {'description': 'Z2 O95409 V595 **p.His3Leu** Dase - Hophaly type 5 (HPE5) [609637]',
'protein': 'p.His3Leu'},
4: {'description': 'Z2 O95409 V595 **p.His4Leu** Dase - Hophaly type 5 (HPE5) [609637]',
'protein': 'p.His4Leu'},
5: {'description': 'Z2 O95409 V595 **p.His5Leu** Dase - Hophaly type 5 (HPE5) [609637]',
'protein': 'p.His5Leu'},
7: {'description': 'Z2 O95409 V595 **p.His7Leu** Dase - Hophaly type 5 (HPE5) [609637]',
'protein': 'p.His7Leu'}}
现在您可以执行如下查找:
>>> print d[3]['protein']
p.His3Leu
>>> print d[3]['description']
Z2 O95409 V595 **p.His3Leu** Dase - Hophaly type 5 (HPE5) [609637]