我有一个端口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'}
我希望字典的键的顺序与列表中的相同(端口号)。你知道吗
在低于3.6版的Python中,不能保证字典中键的顺序。如果您想要这样的行为,您必须使用python3.6或来自
collections
模块的OrderedDict
,正如sniperd所建议的link所示相关问题 更多 >
编程相关推荐