如何维护现有字典python的顺序 [英] How to maintain the order of an existing dictionary python

查看:214
本文介绍了如何维护现有字典python的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个字典,后来我想将其值插入到列表中.我知道列表保持顺序,但我认为字典不是.我知道有OrderedDict,但是据我了解,它在添加到字典中时会保持顺序.在这里,我已经有完整的字典,没有添加.

我正在使用python 3.6

我的脚本是:

dirs_dictionary = {"user_dir_treatment":"/home/dataset_1/treatment",
                           "user_dir_control":"/home/dataset_1/control"}

empty_list = []

for key, value in dirs_dictionary.items()):
    empty_list.append(dirs_dictionary[key])

所以最终,我希望列表包含的值与字典中的顺序相同,这意味着列表中的第一项是"/home/dataset_1/treatment",第二项是"/home/dataset_1/control". /p>

如何维护字典的顺序?

解决方案

在Python 3.6中,字典在内部进行排序,但这被认为是实现细节,不应依赖于此.

在Python 3.7中,字典是有序的.

因此,您有2个选择:

使用实施细节的风险由您自己承担

您可以使用list(d)来检索字典中保持插入顺序的键.

dirs_dictionary = {"user_dir_treatment":"/home/dataset_1/treatment",
                   "user_dir_control":"/home/dataset_1/control"}

empty_list = list(dirs_dictionary)

print(empty_list)

# ['user_dir_treatment', 'user_dir_control']

使用OrderedDict

from collections import OrderedDict

dirs_dictionary = OrderedDict([("user_dir_treatment", "/home/dataset_1/treatment"),
                               ("user_dir_control", "/home/dataset_1/control")]

empty_list = list(dirs_dictionary)

print(empty_list)

# ['user_dir_treatment', 'user_dir_control']

I created a dictionary, and later I'd like in insert it's values to a list. I know that lists keep their order, but I think that dictionaries are not. I know there is OrderedDict, but from what I understand it keeps the order upon addition to the dictionary. Here, I have already the complete dictionary, without adding to it.

I'm using python 3.6

My script is:

dirs_dictionary = {"user_dir_treatment":"/home/dataset_1/treatment",
                           "user_dir_control":"/home/dataset_1/control"}

empty_list = []

for key, value in dirs_dictionary.items()):
    empty_list.append(dirs_dictionary[key])

So eventually, I'd like that the list will contain the values in the same order as they are in the dictionary, meaning that the first item in the list is "/home/dataset_1/treatment", and the second is "/home/dataset_1/control".

How do I maintain the order of my dictionary?

解决方案

In Python 3.6, dictionaries are ordered internally, but this is considered an implementation detail which should not be relied upon.

In Python 3.7, dictionaries are ordered.

Therefore, you have 2 options:

Use the implementation detail at your risk

You can use list(d) to retrieve the keys of the dictionary maintaining insertion order.

dirs_dictionary = {"user_dir_treatment":"/home/dataset_1/treatment",
                   "user_dir_control":"/home/dataset_1/control"}

empty_list = list(dirs_dictionary)

print(empty_list)

# ['user_dir_treatment', 'user_dir_control']

Use OrderedDict

from collections import OrderedDict

dirs_dictionary = OrderedDict([("user_dir_treatment", "/home/dataset_1/treatment"),
                               ("user_dir_control", "/home/dataset_1/control")]

empty_list = list(dirs_dictionary)

print(empty_list)

# ['user_dir_treatment', 'user_dir_control']

这篇关于如何维护现有字典python的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆