简体   繁体   English

在Python中将(单个列表)与(列表列表)进行比较

[英]Compare a (single small list) with a (list of lists) in python

I am working with python 3.6. 我正在使用python 3.6。 I have a list coordinates = [101758584, 101837149, 101844851] and another list of lists rows = [['1', '36933096', 'CSF3R', 'chr1:g.36933096T>C', 'Pathogenic', 'Tarceva\\n'], ['2', '25463483', 'DNMT3A', 'chr2:g.25463483G>A', 'Pathogenic', 'Tarceva\\n'], ['2', '25469502', 'DNMT3A', 'chr2:g.25469502C>T', 'risk factor', 'Iressa\\n']].... and this list goes on . 我有一个列表coordinates = [101758584, 101837149, 101844851] ,另一个列表rows = [['1', '36933096', 'CSF3R', 'chr1:g.36933096T>C', 'Pathogenic', 'Tarceva\\n'], ['2', '25463483', 'DNMT3A', 'chr2:g.25463483G>A', 'Pathogenic', 'Tarceva\\n'], ['2', '25469502', 'DNMT3A', 'chr2:g.25469502C>T', 'risk factor', 'Iressa\\n']].... and this list goes on列表rows = [['1', '36933096', 'CSF3R', 'chr1:g.36933096T>C', 'Pathogenic', 'Tarceva\\n'], ['2', '25463483', 'DNMT3A', 'chr2:g.25463483G>A', 'Pathogenic', 'Tarceva\\n'], ['2', '25469502', 'DNMT3A', 'chr2:g.25469502C>T', 'risk factor', 'Iressa\\n']].... and this list goes on I want to check if the numbers present in coordinates are present in the rows list. 我要检查行列表中是否存在坐标中存在的数字。 So far what I have tried is - 到目前为止,我尝试过的是-

coordinates = [101758584, 101837149, 101844851]
rows = [['1', '36933096', 'CSF3R', 'chr1:g.36933096T>C', 'Pathogenic', 'Tarceva\n'], ['2', '25463483', 'DNMT3A', 'chr2:g.25463483G>A', 'Pathogenic', 'Tarceva\n'], ['2', '25469502', 'DNMT3A', 'chr2:g.25469502C>T', 'risk factor', 'Iressa\n']]
    for e in rows:
            if e[0] in coordinates:
                chromo_final.append(e)

        print(chromo_final)

The output for this is an empty list. 此输出是一个空列表。 The second thing that I tried was - 我尝试的第二件事是-

chromo_final=[x for x in rows if x[0] in coordinates]
print(chromo_final)

Even this code gives an empty list. 即使这段代码也给出了一个空列表。 One example of the output is - 输出的一个示例是-

7   101755060   CUX1    chr7:g.101755060A>G Likely pathogenic   Cotellic

The pattern in the coordinates is present on the second position of the output. 坐标中的模式位于输出的第二个位置。 This output can be of many lines as my list of lists is huge. 此输出可能有很多行,因为我的列表列表很大。 I would love to know so as to exactly where I am going wrong and also how can I go about this code to get the correct output. 我很想知道我要去哪里,以及如何处理此代码以获得正确的输出。

一种简单的方法是使用list comprehension ,要指出的一件事是coordinatesrows[1]数据类型不同,并且您的坐标在index-1因此使用x[1]代替x[0] x[1]

>>> [i for i in rows for j in coordinates if i[1] == str(j)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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