Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Two Lists to Find Bigger List

I have two lists and need to compare them by their largest element, if that is tied, their 2nd largest element, and if that is tied, 3rd largest etc iterating to the entire array.

For example:

list1= [0,2,3,6,12]
list2= [1,2,3,6,12]
list3= [1,4,5,8,12]
list4= [1,4,5,9,12]

So list4 > list3 > list2 > list1.

I wrote a function that accomplishes this:

def compare(x,y):
    if sorted(x)==sorted(y):
        return "Tie"
    for index in range(len(x)-1,-1,-1):
        if sorted(x)[index]>sorted(y)[index]:
            return x
        elif sorted(x)[index]<sorted(y)[index]:
            return y

I was wondering if there was a much neater and more efficient way of writing the function because it doesn't seem very Pythonic.

Edit: comparing lists by using "<" and ">" will sort the lists from the smallest index to largest index, not largest index to smallest index. Reversed would make ">" and "<" the simplest solutions.

like image 229
Noob Coder Avatar asked Dec 31 '13 02:12

Noob Coder


People also ask

How do you compare two lists to find differences?

The difference between two lists (say list1 and list2) can be found using the following simple function. By Using the above function, the difference can be found using diff(temp2, temp1) or diff(temp1, temp2) . Both will give the result ['Four', 'Three'] .

Is it possible to compare two lists using comparison operators explain?

Using list. sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.

Can you compare lists with == in Python?

We can club the Python sort() method with the == operator to compare two lists. Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

Is there a way in Excel to compare two lists?

The Quick Way: Highlight Unique Cells to Compare Lists A quick way to compare two lists in your spreadsheet is to use Excel's unique highlight feature. This feature highlights the items in a list that are not found in the other list. This way you know exactly what items are missing from your lists.


1 Answers

How about this?

>>> list1= [0,2,3,6,12]
>>> list2= [1,2,3,6,12]
>>> list3= [1,4,5,8,12]
>>> list4= [1,4,5,9,12]
>>> def sort_lists_by_maxes(*lists):
    return sorted(lists, key=lambda x: sorted(x, reverse=True), reverse=True)

>>> sort_lists_by_maxes(list1, list2, list3, list4)
[[1, 4, 5, 9, 12], [1, 4, 5, 8, 12], [1, 2, 3, 6, 12], [0, 2, 3, 6, 12]]

The lists are compared by their individually sorted values, and you can feed as many lists as you would like into the function as arguments.

like image 71
Justin O Barber Avatar answered Oct 30 '22 13:10

Justin O Barber