简体   繁体   中英

How to effectively compare two lists of dictionaries to declare a “winner”

I have the following data in a list of dictionaries. How can I effectively compare these two according to the following rule, and conclude who the winner is?

if any value in a list turns out to be more than all the other values of the other list , The list with bigger value will be declared the winner

s1=[{'link': 0}, {'link': 0}] 
s2=[{'link': 0}, {'link': 1}]

My Attempt was :

for stat in s2:
    for stat1 in s1:
        if stat['link'] >= stat1['link']:
            print('success')

I assume that you consider the sequencing relevant. Your existing solution would compare every value in s1 against every value in s2.

You probably want to use zip to marry the two sequences together. If you expect the sequences to have different lengths, use itertools.zip_longest instead.

For example:

def compare_same_lengths(s1, s2):
    for i1, i2 in zip(s1, s2):
        if i1['link'] < i2['link']:
            return -1
        elif i1['link'] > i2['link']:
            return 1

    return 0

Edit:

Based on your comment:

if any value in a list turns out to be more than all the other values of the other list , The list with bigger value will be declared the winner

you should probably use max and then just compare the two maxima:

m1 = max(s1, key=operator.itemgetter('link'))
m2 = max(s2, key=operator.itemgetter('link'))

if m1['link'] > m2['link']:
    return 1
elif m2['link'] < m1['link']:
    return -1
else:
    return 0

If you just want to see which list has the biggest number, you can use the builtin max function :

s1=[{'link': 0}, {'link': 0}] 
s2=[{'link': 0}, {'link': 1}]

maxes = max([[item["link"] for item in s1], [item["link"] for item in s2]])

if maxes[0] > maxes[1]:
    print("S1 is greater than S2")
elif maxes[0] < maxes[1]:
    print("S2 is greater than S1")
else:
    print("S1 and S2 are equal")

Perhaps you were trying to break out of the nested loop? In which case, I suggest you wrap your code in a function so you can simply use return once you find an element in s2 greater than one in s1.

Anyway, assuming this is the strict definition:

s2 is greater than s1 because one of the values in s2 turns out to be greater than in s1

One linear solution is to first get the minimum of s1, then iteratively compare that with the elements in s2.

def is_greater(s1, s2):
  s1_min = min(s1, key=lambda x: x['link']) # just `min(s1)` works for python 2.7
  for item in s2:
    if item['link'] > s1_min['link']:
      return True
  return False    

Note that if you have s1 values of [0,1,2,3] and s2 values of [0,1,0,0] that would still result s2 > s1 because at least one of the values in s2 is greater than at least one of the values in s1 (which i think doesn't really make sense as a definition of greater than :P )

EDIT: added key parameter in min function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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