0

我有两个列表:一个只包含数字,而另一个包含字符串。例如:

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]" ]

我想匹配 listA 中的数字和 listB 中的粗体字。最终,当我使用 listA 的数量时,我想打印整个 listB 字符串。例如,如果我在 listA 中使用数字 3,我还想打印出来:Z2 O95409 V595 **p.His3Leu** Dase - Hophaly type 5 (HPE5) [609637]

4

3 回答 3

4

像字典一样?使用zip

listAtoB = dict(zip(listA, listB))

listAtoB[3] # Z2 O95409 V595 **p.His3Leu** Dase - Hophaly type 5 (HPE5) [609637]
于 2012-08-29T00:08:09.407 回答
2

像这样的东西?

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]"]
for x in listA:
    for y in ListB:
        if y[y.index("p.His")+5]==str(x): #if x in "p.HisxLeu"
            print x,y

或使用filter()

for x in listA:
    for y in filter(lambda z:z[z.index("p.His")+5]==str(x),ListB):
        print x,y

输出:

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]
于 2012-08-29T00:09:54.827 回答
1

创建将 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]
于 2012-08-29T00:30:53.900 回答