繁体   English   中英

从字典中获取最大值

[英]Getting the max value from dictionary

这些就是我的关键价值

    dict_info = {}
    dict_info['math 12345'] = 10
    dict_info['math 1234'] = 2
    dict_info['math 123'] = 1
    dict_info['SCI 124'] = 16
    dict_info['SCI 345'] = 2

因此,我有五个不同的字典。 但是我想做的是确保我比较空格前的第一个字母,例如我那里有3个数学和2个SCI。 我只想获得代表MATH 12345的最高值10。获得代表SCI 124的16并跳过其余的值。 因此,我所需要的只是数学和SCI类的最高价值。 并摆脱价值较低的产品。 到目前为止,我的代码看起来像这样。 我不知道语法。

def check_if_works():
    import operator
    dict_info = {}
    dict_info['math 12345'] = 10
    dict_info['math 1234'] = 2
    dict_info['math 123'] = 1
    dict_info['SCI 124'] = 16
    dict_info['SCI 345'] = 2
    for key, value in dict_info.iteritems():
        arr = key.split(' ')
        class_first_letters_only = arr[0]
        if class_first_letters_only == arr[0]:



check_if_works()

@ user161151这是代码,但它将我的json文件中的所有重复项打印出来。

for key,value in new_dictionary.iteritems():
                                            #print key
                                            k = key.split()[0]
                                            full_info = k + ' ' + key.split()[-1]
                                            print full_info
                                            if ans.get(k,0) < value:
                                                ans[k] = value

                                        #print new_dictionary
                                                sort_info = sorted(ans.items(), key=itemgetter(1,0), reverse=True)
                                                first_20 = sort_info[:20]       

        with open('output_1.json','wb') as outfile:
                json.dump(first_20,outfile,indent=4)

我也想让我的输出想要完整的键名,而不是像MATH这样的前缀,我想要MATH 12345:16。我也想将输出保存到一个json文件中,该文件按从最大到最小的顺序排序价值对。 @扬科斯·弗兰卡斯(Jankos Frankas)

for key, value in new_dictionary.iteritems():
                                            try:
                                                if result[key.split()[0]] < value:
                                                    result[key.split()[0]] = value
                                                    keys[key.split()[0]] = key
                                            except KeyError:
                                                result[key.split()[0]] = value
                                                keys[key.split()[0]] = key
                                        #replace the key prefixes with full length key
                                        for key in keys.keys():
                                            result[keys[key]] = result.pop(key)
                                        #return result
                        with open('output_check_123.json','wb') as outfile:
                            outfile.write(json.dumps(new_dictionary,indent=4))

有代码。

返回dict_info的键中每个唯一的第一个单词的dict_info作为键的字典,其最大值对应于dict_info第一个单词。

def get_max_groups(dict_info):
    result = {}
    for key, value in dict_info.iteritems():
        sub_key = key.split()[0]
        match_keys = filter(lambda ikey: ikey.split()[0] == sub_key, result)
        if not match_keys:
            result[key] = value
            continue
        m = match_keys[0]
        if result[m] < value:
            del result[m]
            ans[key] = value
    return ans

结合使用max和一些列表理解

dict_info = {}
dict_info['math 12345'] = 10
dict_info['math 1234'] = 2
dict_info['math 123'] = 1
dict_info['SCI 124'] = 16
dict_info['SCI 345'] = 2

def check_if_works(dictionary):
    math_max = max([dictionary[key] for key in dictionary if key.startswith('math')])
    sci_max = max([dictionary[key] for key in dictionary if key.startswith('SCI')])
    return math_max, sci_max


print check_if_works(dict_info)

尝试这个:

def check_if_works():
    import operator
    dict_info = {}
    dict_info['math 12345'] = 10
    dict_info['math 1234'] = 2
    dict_info['math 123'] = 1
    dict_info['SCI 124'] = 16
    dict_info['SCI 345'] = 2
    result={}
    for key, value in dict_info.iteritems():
        try:
            if result[key.split()[0]] < value:
                result[key.split()[0]] = value
        except KeyError:
            result[key.split()[0]] = value

    return result



dict_info = check_if_works()
print dict_info

现在dict_info等于:

{'SCI':16,'math':10}

如果您需要完整长度的密钥,则不仅“前缀”还使用此代码:

def check_if_works():
    import operator
    dict_info = {}
    dict_info['math 12345'] = 10
    dict_info['math 1234'] = 2
    dict_info['math 123'] = 1
    dict_info['SCI 124'] = 16
    dict_info['SCI 345'] = 2
    result={}
    keys = {}
    for key, value in dict_info.iteritems():
        try:
            if result[key.split()[0]] < value:
                result[key.split()[0]] = value
                keys[key.split()[0]] = key
        except KeyError:
            result[key.split()[0]] = value
            keys[key.split()[0]] = key
    #replace the key prefixes with full length key
    for key in keys.keys():
        result[keys[key]] = result.pop(key)
    return result



dict_info = check_if_works()
print dict_info

结果:

{'math 12345':10,'SCI 124':16}

def check_duplicates(dict_info):
    import operator

    result={}
    keys = {}
    for key, value in dict_info.iteritems():
        try:
            #print result[key.split()[0]]
            if result[key.split()[0]] < value:
                result[key.split()[0]] = value
                keys[key.split()[0]] = key
        except KeyError:
            result[key.split()[0]] = value
            keys[key.split()[0]] = key
    #replace the key prefixes with full length key
    for key in keys.keys():
        result[keys[key]] = result.pop(key)
    return result

这非常感谢@Jankos。 我添加了一个名为dict_info的参数,在这里我可以使用此函数使其在我的主脚本下工作,该主脚本具有与所示字典相同的格式。 现在可以正常使用了,谢谢!

暂无
暂无

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

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