简体   繁体   English

如何在python中的列表列表中比较项目

[英]How to compare an item within a list of list in python

I am a newbie to python and just learning things as I do my project and here I have a list of lists which I need to compare between the second and last column and get the output for the one which has the largest distance. 我是python的新手,在做项目时会学习一些东西,这里有一个列表列表,我需要在第二列和最后一列之间进行比较,以获取距离最大的列的输出。 Moreover, I am replicating a list. 而且,我正在复制一个列表。 If someone could help me to do it within a single list of lists in ag it could be really helpful. 如果有人可以帮我在列表中的一个列表中做到这一点,那将真的很有帮助。 Thanks in advance 提前致谢

if this is an INPUT then the output should be, 如果这是一个INPUT,那么输出应该是

ag = [['chr12','XX',1,5,4],
     ['chr12','XX',2,5,3],
     ['chr13','ZZ',6,10,4],
     ['chr13','ZZ',8,9,1],
     ['ch14','YY',12,15,3],['ch14','YY',12,15,3]]

EXPECTED OUTPUT:

     ['chr12','XX',1,5,4]
     ['chr13','ZZ',6,10,4]
     ['ch14','YY',12,15,3]

#However I tried of replicating the list like
#INPUT
ag = 
 [['chr12','XX',1,5,4],
 ['chr12','XX',2,5,3],
 ['chr13','ZZ',6,10,4],
 ['chr13','ZZ',8,9,1],
 ['ch14','YY',12,15,3],
 ['ch14','YY',12,15,3]]
 bg = 
 [['chr12','XX',1,5,4],
 ['chr12','XX',2,5,3],
 ['chr13','ZZ',6,10,4],
 ['chr13','ZZ',8,9,1],
 ['ch14','YY',12,15,3],
 ['ch14','YY',12,15,3]]


#The code which I tried was

c= []
for i in ag:
 for j in bg:
    if i[0]==j[0] and i[1]==j[1] and i[4]>j[4]:
        c.append(i)


the output which i get is
[['chr12', 'XX', 1, 5, 4], ['chr13', 'ZZ', 6, 10, 4]]

In short : To compare items in an iterable (eg list) of lists, use the keyword agument key of the max/min function. 简而言之 :要比较列表的可迭代(例如列表)中的项目,请使用max / min函数的关键字agument key It takes a function or lambda expression and compares the given values by the result of the key function when given each value. 它采用一个函数或lambda表达式,并在给定每个值时将其与key函数的结果进行比较。


Assuming that what you really want is to reduce a list of lists so that the entries' second elements are unique and that you want the last elements to determin which entry to keep in case of redundant 2nd values: 假设您真正想要的是减少列表的列表,以便条目的第二个元素是唯一的,并且您希望最后一个元素确定在冗余第二个值的情况下保留哪个条目:

If there is any problem regarding iteration, itertools has the answer. 如果有关迭代有任何问题, itertools会提供答案。 In this case, we just need the groupby method and standard Python's max method with the keyword argument key . 在这种情况下,我们只需要带有关键字参数keygroupby方法和标准Python的max方法。

from itertools import groupby

def filter(matrix):
    filtered = [] # create a result list to hold the rows (lists) we want
    for key, rows in groupby(matrix, lambda row: row[1]): # get the rows grouped by their 2nd element and iterate over that
        filtered.append(max(rows, key=lambda row: row[-1])) # add the line that among its group has the largest last value to our result
    return filtered # return the result

We could squeeze this into a single generator expression or list comprehension but for a beginner, the above code should be complex enought. 我们可以将其压缩为单个生成器表达式或列表理解,但是对于初学者来说,上面的代码应该足够复杂。

Please be sure to follow Stack Overflow's guidelines for future questions to prevent low ratings and ensure prompt and high quality answers. 对于将来的问题,请务必遵循Stack Overflow的指南 ,以防止评分较低,并确保及时提供高质量的答案。

Your list ag and bg are completely duplicating, so I give this example for your issue. 您的清单agbg完全重复,因此我举一个例子说明您的问题。 Hope it help. 希望对您有所帮助。

>>>ag = [['chr12','XX',1,5,4],['chr12','XX',2,5,3],['chr13','ZZ',6,10,4],['chr13','ZZ',8,9,1],['ch14','YY',12,15,3]]
>>>bg = [['chr12','XX',1,5,4],['chr12','XX',2,5,3],['chr13','ZZ',6,10,4],['chr13','ZZ',8,9,1]]
>>>[i for i in ag + bg if i not in ag or i not in bg]
[['ch14', 'YY', 12, 15, 3]]

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

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