遍历字典,维护构造的lis的顺序

2024-09-30 01:22:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个端口ID列表,顺序正确。基于此,我从一个文件中获取值并将其存储在字典中。但是字典中存储的值与原始列表不一致。这是我的密码:

import os
os.chdir('/var/lib/docker/volumes/kolla_logs/_data/openvswitch/')
port_ids=['qvoee855b93-ba', 'qvo9aa3a7d8-64', 'qvo2fc6e482-aa', 'qvo6a27cf40-8f']

def port_numb(text):
    try:
        with open('ovs-vswitchd.log') as f:
            for line in f:
                if line.find(text) != -1:
                    return line[97:100]
    except Exception as ex:
        print('Failed to open file {}'.format(ex))

ovs_port_numb = list(map(port_numb, port_ids))
d = dict(zip(port_ids, ovs_port_numb))
print d

输出: {'qvoee855b93-ba':'174','qvo2fc6e482 aa':'176','qvo6a27cf40-8f':'177','qvo9aa3a7d8-64':'175'}

我希望字典的键的顺序与列表中的相同(端口号)。你知道吗


Tags: ids列表字典顺序osportlineaa

热门问题