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. 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,
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 wasc= []
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 (e.g. list) of lists, use the keyword agument key
of the max/min function. It takes a function or lambda expression and compares the given values by the result of the key
function when given each value.
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. In this case, we just need the groupby
method and standard Python's max method with the keyword argument key
.
from itertools import groupbydef filter(matrix):filtered = [] # create a result list to hold the rows (lists) we wantfor key, rows in groupby(matrix, lambda row: row[1]): # get the rows grouped by their 2nd element and iterate over thatfiltered.append(max(rows, key=lambda row: row[-1])) # add the line that among its group has the largest last value to our resultreturn 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.