【问题标题】:How do I display the key and updated values together obtained from a dictionary?如何显示从字典中获得的键和更新值?
【发布时间】:2019-05-23 09:06:36
【问题描述】:

我的代码应该删除字典中长度为 7 个或更少字符的同义词,一旦完成所有删除,我将需要按键顺序显示它。但是,我不知道如何将密钥和更新的值放在一起。预期的输出应该是这样的:

(按字母顺序排序):

dangerous : ['hazardous', 'perilous', 'uncertain'] 
show : ['communicate', 'manifest', 'disclose']
slow : ['leisurely', 'unhurried']
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
             'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack'],
             'dangerous': ['perilous', 'hazardous', 'uncertain']}

def main():
    edited_synonyms = remove_word(word_dict)
    key_order(edited_synonyms)

def remove_word(word_dict):
    dictionary = {}

    synonyms_list = word_dict.values()
    new_list = []
    for i in synonyms_list:
        new_list.extend(i)

    for word in new_list:
        letter_length = len(word)
        if letter_length <= 7:
            new_list.pop(new_list.index(word))

    value = new_list 
    keys_only = word_dict.keys()
    key = keys_only
    dictionary[key] = value
    return dictionary


def key_order(word_dict):
    word_list = list(word_dict.keys())
    word_list.sort()
    for letter in word_list:
        value = word_list[letter]
        print(letter, ": ", value)

main()

到目前为止发生的一个错误是“TypeError: unhashable type: 'dict_keys'”

【问题讨论】:

  • dicts 无法排序。你应该使用OrderedDict

标签: python list dictionary for-loop key-pair


【解决方案1】:
word_dict = {'show': ['display', 'exhibit', 'convey', 'communicate', 'manifest', 'disclose'],
             'slow': ['unhurried', 'gradual', 'leisurely', 'late', 'behind', 'tedious', 'slack'],
             'dangerous': ['perilous', 'hazardous', 'uncertain']}

result = {i:list(filter(lambda x: len(x)>7, word_dict[i])) for i in word_dict }

key_sorted =sorted(result.keys(), key=lambda x:x.lower())
# sorting alphabetically
solution = {i:result[i] for i in key_sorted}

print(solution)

output

{'dangerous': ['perilous', 'hazardous', 'uncertain'], 
 'show': ['communicate', 'manifest', 'disclose'],
 'slow': ['unhurried', 'leisurely']
 }

【讨论】:

    【解决方案2】:

    你的问题可以用一条线解决:

    waka_dict = {key: [w for w in word_dict[key] if len(w) &gt; 7] for key in word_dict}

    它将返回所需的字典:

    {'dangerous': ['perilous', 'hazardous', 'uncertain'],
     'show': ['communicate', 'manifest', 'disclose'],
     'slow': ['unhurried', 'leisurely']}
    

    (它将保存原始列表顺序)


    如果你想让你的 dict 有序,你应该记住 Python dicts 是无序的。你应该使用collections.OrderedDict

    from collections import OrderedDict
    
    ordered_dict = OrderedDict(sorted(waka_dict.items()))
    

    【讨论】:

      【解决方案3】:
          value = new_list 
          keys_only = word_dict.keys()
          key = keys_only
          dictionary[key] = value
      

      这是引发错误的代码。 key 不是单个键,它是 dict_keys 类型的对象(由 word.dict.keys() 返回)。这样的对象不能是单个键,因为字典使用哈希映射,并且就像错误所说的那样,dict_keys 是不可哈希的。

      最简单的解决方案(保留您的代码)是将您的整个函数放在一个循环中,首先对键进行迭代,以便在每次迭代时只处理一个字典条目。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 1970-01-01
        • 2019-07-15
        相关资源
        最近更新 更多