-2

I am trying to compare elements in two lists. Both of the lists contain numbers and are sorted from greatest to least. I want to find the list with the highest number. If they contain the same highest number, I want to look at the next highest number, etc.

So if I had a list: [14, 5, 4, 3, 2]

And I was comparing it to: [14, 7, 4, 3, 2]

The second list would be bigger because the next highest number is a 7.

Likewise, if I had a list: [13, 12, 9, 7, 3]

And: [13, 12, 9, 8, 2]

The second would again be the larger of the two.

Any help would be appreciated!

I tried one of these suggestions:

def compare_high_card(hand_a,hand_b): ''' Determines which hand has the highest high-card, returns 1 if hand_a has higher card, -1 if hand_b has higher_card :param hand_a: The first hand to compare :param hand_b: The second hand to compare :return: 1 if hand_a has higher card, -1 if hand_b has higher_card '''

hand_a = sort_hand_by_value(hand_a)
hand_b = sort_hand_by_value(hand_b)

hand_length = 5

for index in range(hand_length):

    if hand_a[index] > hand_b[index]:
        higher_hand =  1
        break
    elif hand_b[index] > hand_a[index]:
        higher_hand = -1
        break
    else:
        higher_hand = 0

return higher_hand

hand_a = [14, 9, 4, 3, 2] hand_b = [14, 8, 5, 3, 2]

This code only prints out -1.

B. Lop
  • 31
  • 1
  • 5
  • While not precisely the same question, this should help: https://stackoverflow.com/q/13052857/9209546 – jpp Jan 26 '18 at 00:42

5 Answers5

6

If the lists are sorted, simply do:

a = [14, 5, 4, 3, 2]
b = [14, 7, 4, 3, 2]

print(a > b)

Ref: Comparing Sequences and Other Types:

"Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted"

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
1
A=[14, 5, 4, 3, 2]
B=[14, 7, 4, 3, 2]

for x in range(0,len(A)):
    if A[x]>B[x]:
      print("A is bigger")
      break
    elif A[x]<B[x]:
      print("B is bigger")
      break
    elif x==len(A)-1:
      print("The arrays are equal")

Edit: Straightforward way:

A=[14, 5, 4, 3, 2]
B=[14, 7, 4, 3, 2]

if A>B:
    print("A is bigger")
elif A<B: 
    print("B is bigger")
else:
    print("Both are the same")
david.t_92
  • 1,971
  • 1
  • 11
  • 15
0

You can use any:

a = [14, 5, 4, 3, 2]
b = [14, 7, 4, 3, 2]
print(any(i > c for i, c in zip(a, b)) #checking if a > b
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

For unsorted lists:

testListA = [13, 12, 9, 7, 3, 30]
testListB = [13, 12, 9, 8, 2]


def getbiggerlist(list_a: list, list_b: list):
    return sorted(list_a) > sorted(list_b)


print(getbiggerlist(testListA, testListB))
Sam Bokai
  • 538
  • 1
  • 5
  • 13
0

Here is recursive approach:

data1=[13, 12, 9, 7, 3]

data2=[13, 12, 9, 8, 2]


def comapre(lst1,lst2):
    vb=sorted(lst1,reverse=True)
    rb=sorted(lst2,reverse=True)


    ss=max(vb)
    pp=max(rb)
    if ss==pp:

        return comapre(vb[1:],rb[1:])
    elif ss>pp:
        return data1
    else:
        return data2
print(comapre(data1,data2))

output:

[13, 12, 9, 8, 2]