在使用max(iterable)时,是否可能收到平局通知?

kzmpq1sx  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(203)

现在,我有这样的想法:

values = {}
for i in message.reactions:
    itervalues = {str(i): int(i.count)}
    values.update(itervalues)
print(values)
max_key = max(values, key=values.get)
print(max_key)
all_values = values.values()
max_value = max(all_values)
print(max_value)

具有 values{} 存在 {':one:': 2, ':two:': 1, ':three:': 2, ':four:': 1, ':five:': 1} (检查不一致消息的React)
在这个例子中,它总是给出 :one: 作为具有最高值的键,即使它与 :three:

vbopmzt1

vbopmzt11#

你不能用电脑检测领带 max (据我所知),但你可以这样做:


# rewrote your dictionary creation as a one-line dict comprehension.

# I also renamed it, as I find calling a dictionary "values" to be very confusing,

# given that dictionaries have a method of the same name!

mydict = {str(i): int(i.count) for i in message.reactions}

# get the maximum value in the dictionary

max_val = max(my dict.values())

# get a list of all the keys that have the maximum value in the dictionary

highest_keys = [key for key, val in mydict.items() if val == max_val]

print(mydict)
print(highest_keys)
print(max_val)

如果您只想检测领带(并且对系的键不感兴趣),您可以这样做 sum(1 for val in mydict.values() if val == max_val) ,并测试该表达式的结果是否大于1。

相关问题