【问题标题】:How to change dictionary format in python?如何在python中更改字典格式?
【发布时间】:2022-12-02 18:28:27
【问题描述】:

I need to transform dict { name : department } to { department : [ name ] } and print all names after transformation, but it prints me only one, what is wrong here?

Tried this, but it doesn\'t work as expected:

orig_dict = {\'Tom\': \'HR\', \'Ted\': \'IT\', \'Ken\': \\
             \'Marketing\', \'Jason\': \'Marketing\', \'Jesica\': \'IT\', \'Margo\': \'IT\', \'Margo\': \'HR\'}

new_dict = {value: [key] for key, value in orig_dict.items()}

it_names = new_dict[\'IT\']

print(it_names)

    标签: python dictionary


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2022-08-03
    • 2021-05-30
    • 2017-02-03
    • 2019-07-17
    相关资源
    最近更新 更多