I might use collections.Counter for this.
A Counter is a dict subclass that counts objects. In the resulting dictionary, the keys are the objects being counted while the values are the number of times those objects appear.
We can create a count of dice rolls by passing a list of the results of random.randint() to Counter's constructor, like so:
Counter(random.randint(1,6) for _ in range(3)) # 3 6-sided rolls
In addition to accessing the counts via dict's [] operator, we can also access them via Counter.most_common(), which returns a list containing all of the rolls and their associated counts.
In our program, rolls[0] is the most common roll and its count, rolls[0][0] is the value of the roll, rolls[0][1] is the number of times it was rolled. Similarly, rolls[1][0] is the second-most common value.
import random
from collections import Counter
rolls = Counter(random.randint(1,6) for _ in range(3)).most_common()
if len(rolls) == 1:
score = rolls[0][0] * 3
elif len(rolls) == 2:
score = rolls[0][0] * 2 - rolls[1][0]
else:
score = 0
print (rolls, score)
You haven't specified what the correct score is when there are no matches. The above algorithm gives a score of 0 if there are no matches.
Here is another algorithm which scores the negative sum of all the dice if there are no matches. In essence, it adds up all of the matching dice and subtracts each singleton die. This gives the answer you request for each of the cases you describe but punishes non-matching rolls more severely. Notice how this version generalizes easily to different numbers of dice and different numbers of sides.
import random
from collections import Counter
rolls = Counter(random.randint(1, 6) for _ in range(3)).most_common()
score = sum(roll[0] * (-1 if roll[1]==1 else 1) * roll[1] for roll in rolls)
print(rolls, score)
score = 0@timgeb