-1

我有一个字典列表,它是使用我使用 netmiko 一个 python 库创建的工具构建的。目前我能够在字典列表中获取数据,如下所示:

output_list = [{2: ['sjc01-0001-lsw01a', 'DCS-7280SR-48C6-R', '4.22.2.1F']}, {1: ['sjc01-0001-lsw01b', 'DCS-7280SR-48C6-R', '4.22.2.1F']}, {0: ['sjc01-0002-lsw02a', 'DCS-7280SR-48C6-R', '4.22.2.1F']}]

在这种情况下,我希望使用 0,1,2 的键值对 output_list 进行排序,然后打印,因此输出如下所示:

{0: ['sjc01-0002-lsw02a', 'DCS-7280SR-48C6-R', '4.22.2.1F']}
{1: ['sjc01-0001-lsw01b', 'DCS-7280SR-48C6-R', '4.22.2.1F']}
{2: ['sjc01-0001-lsw01a', 'DCS-7280SR-48C6-R', '4.22.2.1F']}
4

1 回答 1

0

我相信这会奏效:

d = {}
for i in range(len(output_list)):
    d.update(output_list[i])
k = list(d.keys())
k.sort()
for each in k:
    print('{{{0}: {1}}}'.format(each, d[each]))
于 2020-07-18T04:12:27.417 回答