在列表中找到最高的一对骰子

Finding highest pair of dice in list

我正在使用 Python 编写一个简单的 Yatzy 应用程序,但我在寻找最佳骰子对时遇到了一些困难。这是函数的概要:

Input: list containing five dice.
Output: highest sum of two dice with the same number. If no pairs found, then -1.

使用 Python 编写此函数的最佳方式是什么?我怎样才能扩大规模,比如两对或满屋?

提前致谢。

这是一个使用集合模块的Python3解决方案。

from collections import Counter
from random import randint
roll = [randint(1, 6) for x in range(5)]

result = max([x for x, cnt in Counter(roll).items() if cnt==2] or [-1])

print(roll, '->', result)

顺便说一下,这里有一个极端情况(4 种 = 2 对),根据您需要的结果,您可能想要比较 cnt > 1