如何将字典值从最高到最低排序?

时间:2018-07-13 12:47:16

标签: python python-3.x sorting dictionary key

我的问题是如何根据最高分数撤出冠军? 由于您无法按字典排序 我用列表尝试了这个,但是然后名字不会出现,只有分数...

a = {'name_a':0}
b = {'name_b':0}
c = {'name_c':0}
d = {'name_d':0}
e = {'name_e':0}

print("Each time someone scores a point, the letter of his name is typed in lowercase. If someone loses a point, the letter of his name is typed in uppercase")


score = input('Enter series of charachters indicating who scored a poitn: ')

for i in score:
    if i == 'a':
        a['name_a'] += 1
    if i == 'A':
        a['name_a'] -= 1
    if i == 'b':
        b['name_b'] += 1
    if i == 'B':
        b['name_b'] -= 1
    if i == 'c':
        c['name_c'] += 1
    if i == 'C':
        c['name_c'] -= 1
    if i == 'd':
        d['name_d'] += 1
    if i == 'D':
        d['name_d'] -= 1
    if i == 'e':
        e['name_e'] += 1
    if i == 'E':
        e['name_e'] -= 1

print(a,b,c,d,e)

print('Winner is: ', )

4 个答案:

答案 0 :(得分:1)

这将起作用:

max((i, name) for d in (a,b,c,d,e) for name, i in d.items())[1]

答案 1 :(得分:0)

max_key = ""
max_val = 0

for key, value in d.items():
    if (value > max_val):
        max_val = value
        max_key = key

这是你的意思吗?

答案 2 :(得分:0)

您可能要使用一个字典,而不是每个字典,例如:

    public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(LoginViewModel model)
    {

        if(ModelState.IsValid)
        {
            return RedirectToAction("Landing", "Start");
        }
        return View();
    }

然后您可以跟踪得分最高的球员:

scores = {
    'a': 0,
    'b': 0,
    'c': 0,
    'd': 0,
    'e': 0,
}

答案 3 :(得分:0)

我找到了答案

OPTIONS
missingMerge -- Distribute counts for missing values. Counts are distributed across other values in proportion to their frequency. Otherwise, missing is treated as a separate value.

binarizeNumericAttributes -- Just binarize numeric attributes instead of properly discretizing them.

doNotCheckCapabilities -- If set, evaluator capabilities are not checked before evaluator is built (Use with caution to reduce runtime).