0

how to compare more then one list, below works ok for just 2 lists but what if more needed

while (list1[0:1] == list2[0:1]):
    if list1[1:2] > list2[1:2]:
        print('list 1 wins')
    elif list1[2:3] > list2[2:3]:
        print('list 1 wins!')
    elif list1[3:4] > list2[3:4]:
        print('list 1 wins')
    elif list1[4:5] > list2[4:5]:
        print('list 1 wins')
    elif list1[5:5] == list2[5:5]:
        print('its a tie')
    else:
        print('list 2 wins')
    break
5
  • 1
    why use while ? in this case while works like if because of break Commented Sep 12, 2015 at 15:47
  • Lowest or highest, you use both in your description. Currently it is testing for highest. Commented Sep 12, 2015 at 16:02
  • Do you actually need to associate a unique string to each separate list - (so that it can be printed in like your example? Commented Sep 12, 2015 at 16:19
  • sum(candidate1) -> 48 Commented Sep 12, 2015 at 16:45
  • Are you always comparing just two totals? or could you compare totals from say five candidates? or n candidates? Commented Sep 12, 2015 at 16:58

4 Answers 4

1

Lists and tuples can be compared just like that assuming that the matching elements are comparable:

>>> list1=[50,9,5,4,5]
>>> list2=[50,9,8,1,5]
>>> list1 > list2
False
>>> list1 < list2
True

That also means that you can order any number of lists:

>>> lists = [list1, list2, list3]
>>> lists.sort()
Sign up to request clarification or add additional context in comments.

1 Comment

sorted(lists) or lists.sort() ...?
1

As pointed out lists can be directly compared without iterating through them.
But when you want to work with multiple lists at the same time it is idiomatic to use zip(), e.g.:

for i1, i2 in zip(list1, list2):
    if i1 > i2:
        print("List 1 wins")
        break
    if i2 > i1:
        print("List 2 wins")
        break
else:
    print("It's a tie")

Comments

0

Did you consider using a for-loop?

The syntax is:

for value in list:
    # do something
    print value

and then there is the (zip function)

list1 = [1,2]
list2 = [a,b]
zip(list1, list2) # = [(1,a),(2,b)] 

With these two tools one is able to provide a really pythonic solution to your problem.

Comments

0

As bereal says, you can just use list comparison for this. If ties weren't an issue, you could just use max() to determine a winner, but to handle ties you need to sort the whole collection of lists.

Eg, this code will handle any number of lists.

list1 = [50,9,5,4,5]
list2 = [50,9,8,1,6]
list3 = [50,9,8,1,5]

lists = [(seq, i) for i, seq in enumerate((list1, list2, list3), 1)]
lists.sort(reverse=True)

if lists[0][0] == lists[1][0]:
    print("it's a tie")
else:
    print("list {0} wins: {1}".format(lists[0][1], lists[0][0]))

output

list 2 wins: [50, 9, 8, 1, 6]

If you change list2 back to [50, 9, 8, 1, 6], then it's a tie will get printed.


lists is a list of tuples, with each tuple containing one of the original lists and its index number, so we can identify lists after the tuples have been sorted. The sorting will compare the lists, and if two lists are identical, then the indices will also be compared, but that doesn't affect the desired outcome here.

BTW, list1[1:2] > list2[1:2] is wasteful. It creates a new pair of single element lists & compares them. It's easier to just compare the elements themselves, eg list1[1] > list2[1].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.