English 中文(简体)
根据价值清单,在平时采用字典
原标题:Reorder dictionary in python according to a list of values

Let us consider a dictionary:

sample_dict={1: r099 ,2: g444 ,3: t555 ,4: f444 ,5: h666 }

我想按照载有我所希望的字典钥匙顺序的清单中具体规定的顺序重新排列该词典。 让我们说,理想的秩序清单是:

desired_order_list=[5,2,4,3,1]

因此,我希望我的独裁者也这样做:

{5: h666 ,2: g444 ,4: f444 ,3: t555 ,1: r099 }

如果我能得到一份价值清单,也是罚款的。 结果是:

[ h666 , g444 , f444 , t555 , r099 ]

我如何以尽可能最复杂的方式做到这一点?

问题回答

Answer for-030 3.6+

Guido已经向独裁者保证,从33.7岁起,他们已经成为3.6个实验性特征。 答案已在上扩大。 • est3.7 + dictionary

在这种情况下,根据<代码>desired_order_list<<>/code>所载项目,用简单的字典理解建立一个新字典。

sample_dict = {1:  r099 , 2:  g444 , 3:  t555 , 4:  f444 , 5:  h666 }
print(sample_dict)
>>> {1:  r099 , 2:  g444 , 3:  t555 , 4:  f444 , 5:  h666 }

desired_order_list = [5, 2, 4, 3, 1]

reordered_dict = {k: sample_dict[k] for k in desired_order_list}
print(reordered_dict)
>>> {5:  h666 , 2:  g444 , 4:  f444 , 3:  t555 , 1:  r099 }

如果你重新使用<条码>,则你可以这样做。

for key in [5,2,4,3,1]:
    my_ordered_dict[key] = my_ordered_dict.pop(key)

顺便说一句,你按照你所希望的顺序,把一切重新列入你命令的字句中,这样以后你就可以做。

my_ordered_dict.values()

并请你就此提出清单。

如果您在<条码>栏中总结重新插入:......;除关键词外:通过,您可重新排列<条码>。 即便不是你名单上的所有关键人物也在场。

除在<>清单不完整的这一特殊情况下外,现有答案还不止涵盖这一问题。 然后,在创建字典之后,update,加上原有的数值,添加缺失的数值:

    sample_dict = {1:  r099 , 2:  g444 , 3:  t555 , 4:  f444 , 5:  h666 }
    print(reordered_dict)
    # {1:  r099 , 2:  g444 , 3:  t555 , 4:  f444 , 5:  h666 }
    desired_order_list = [5, 2 ]
    reordered_dict = {k: sample_dict[k] for k in desired_order_list}
    print(reordered_dict)
    # {5:  h666 , 2:  g444 }
    reordered_dict.update(sample_dict)
    print(reordered_dict)
    # {5:  h666 , 2:  g444 , 1:  r099 , 3:  t555 , 4:  f444 }

弹 Python没有秩序。

http://docs.python.org/dev/library/collections.html# Collections.OrderedDict”rel=“nofollow”>OrderedDict

https://stackoverflow.com/questions/5925731/reorder-dictionary-in-python-according-to-a-list-of- Values/5925766#5925766" 解决办法可能是一个良好的途径,但此处提及的是获得你希望的价值清单的简单方式:

[sample_dict[k] for k in desired_order_list]

如果您完全相信,<代码>desired_order_list 中的每一项内容都将是sample_dict的关键内容,则使用[sample_dict.get(k) ...]。 第一种方法将为缺失的钥匙设置<代码>None,第二种方法只包括钥匙的数值。

改写字典对你的意思是什么? 密码是按其性质分列的数据结构,用于lookup。 页: 1

你们是否希望在某些具体秩序中对独裁者 over? 随后仅使用<代码>desired_order_list :

for key in desired_order_list: 
  # d is the dictionary
  # do stuff with d[key]

正如其他人已经提到的那样,Sharma有一个OrderedDict(在2.7或3.x中),但我不认为它符合你在这方面的需要。 “重组”效率太低。 最好把你的词典与按预期顺序排列的关键清单一起。

如果您仍然坚持<代码>OrderedDict,则仅创设一个新的<代码>OrderedDict,在desired_order_list 上添加该数值。

这样做会比较容易吗?

sample_dict={1: r099 ,2: g444 ,3: t555 ,4: f444 ,5: h666 }
new_sample_dict={
    1: sample_dict[5],
    2: sample_dict[2],
    3: sample_dict[4],
    4: sample_dict[3],
    5: sample_dict[1]
}

使用SortedDict provided by django (). <代码>SortedDict 储存在keyOrder属性(只是一份清单,因此,你可以随时重订。

如果您没有安装或没有使用过django,就只解除执行django.utils.datatstructures。 这取决于 d格的任何其他部分。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签