从字典

时间:2016-04-20 06:26:03

标签: python 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的最高值。得到16代表SCI 124并跳过其余部分。所以我想要的是数学和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))

有代码。

4 个答案:

答案 0 :(得分:2)

返回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

答案 1 :(得分:1)

maxlist comprehension

一起使用
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)

答案 2 :(得分:0)

试试这个:

if (sheet.getCell(j, i).getType() == CellType.NUMBER)

现在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 等于:

  

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

如果你想要全长密钥,不仅“前缀”使用这个代码:

dict_info

resul:

  

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

答案 3 :(得分:0)

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的参数,我可以使用此函数使其在我的主脚本下工作,该脚本具有与呈现的字典相同的格式。现在它完美无缺,谢谢!