简体   繁体   English

在 Python 字典相关任务中需要帮助

[英]Need Help in Python Dictionary Related Task

Consider a dictionary,考虑一本字典,

dictionary = {
   'AE':{'Applied':4, 'Programming':3 }, 
   'BE':{'Applied':4, 'Programming':2 }, 
   'CE':{'Applied':4, 'Programming':5 },
}

With comprehension in definition, yield the output as,在定义理解的情况下,产生 output 为,

output = [['AE':'BE':'CE'], 
          ['Applied', 2,4,5],
          ['Programming', 5,3,2]]

You can use this code:您可以使用以下代码:

dictionary = {
   'AE':{'Applied':4, 'Programming':3 }, 
   'BE':{'Applied':4, 'Programming':2 }, 
   'CE':{'Applied':4, 'Programming':5 },
}

output = []
dict_keys = list(dictionary.keys())
output.append(dict_keys)

for key in dictionary[dict_keys[0]]:
    value = [dictionary[k][key] for k in dict_keys]
    value.insert(0, key)
    output.append(value)

Output: Output:

output = [['AE', 'BE', 'CE'], ['Applied', 4, 4, 4], ['Programming', 3, 2, 5]]

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

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