简体   繁体   English

如何有效地比较两个词典列表以声明“获胜者”

[英]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. 您现有的解决方案会将s1中的每个值与s2中的每个值进行比较。

You probably want to use zip to marry the two sequences together. 您可能想使用zip将两个序列结合在一起。 If you expect the sequences to have different lengths, use itertools.zip_longest instead. 如果您希望序列具有不同的长度,请改用itertools.zip_longest

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: 您应该使用max ,然后比较两个最大值:

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 : 如果您只想查看哪个列表具有最大数目,可以使用内置的max函数

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. 在这种情况下,建议您将代码包装在一个函数中,以便一旦在s2中找到一个大于s1中的元素的元素,就可以简单地使用return

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 s2大于s1,因为s2中的值之一大于s1

One linear solution is to first get the minimum of s1, then iteratively compare that with the elements in s2. 一个线性解决方案是首先获得s1的最小值,然后将其与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 ) 请注意,如果您拥有[0,1,2,3]的s1值和[0,1,0,0]的s2值,那仍将导致s2> s1,因为s2中的至少一个值大于at s1中的值中的至少一个(我认为作为大于:P的定义没有任何意义)

EDIT: added key parameter in min function 编辑:在min函数中添加了关键参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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