使用2个列表

时间:2012-08-29 00:01:09

标签: python

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

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]

3 个答案:

答案 0 :(得分:4)

在字典中?使用zip

listAtoB = dict(zip(listA, listB))

listAtoB[3] # Z2 O95409 V595 **p.His3Leu** Dase - Hophaly type 5 (HPE5) [609637]

答案 1 :(得分: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]

答案 2 :(得分:1)

创建一个字典可以很容易地将listA中的项目映射到ListB中的项目。如果您使用的是Python&gt; = 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]