Python 按另一本词典对词典排序

Python 按另一本词典对词典排序,python,sorting,dictionary,Python,Sorting,Dictionary,我在从字典中排序列表时遇到了一个问题。 我有这个清单 list = [ d = {'file_name':'thisfile.flt', 'item_name':'box', 'item_height':'8.7', 'item_width':'10.5', 'item_depth':'2.2', 'texture_file': 'red.jpg'}, d = {'file_name':'thatfile.flt', 'item_name':'teapot', 'item_hei

我在从字典中排序列表时遇到了一个问题。 我有这个清单

list = [
    d = {'file_name':'thisfile.flt', 'item_name':'box', 'item_height':'8.7', 'item_width':'10.5', 'item_depth':'2.2', 'texture_file': 'red.jpg'},
    d = {'file_name':'thatfile.flt', 'item_name':'teapot', 'item_height':'6.0', 'item_width':'12.4', 'item_depth':'3.0' 'texture_file': 'blue.jpg'},
    etc.
]
我试着在列表中循环,然后

  • 从每个字典创建一个包含字典中项目的新列表。(当用户做出选择时,它会改变哪些项目以及需要添加到列表中的项目数量
  • 对列表排序
当我说sort时,我想象着创建一个这样的新词典

order = {
    'file_name':    0,
    'item_name':    1, 
    'item_height':  2,
    'item_width':   3,
    'item_depth':   4,
    'texture_file': 5
}
['thisfile.flt', 'box', '8.7', '10.5', '2.2']
['thatfile.flt', 'teapot', '6.0', '12.4', '3.0']
['thisfile.flt', 'box', '8.7', '10.5', 'red.jpg']
['thatfile.flt', 'teapot', '6.0', '12.4', 'blue.jpg']
它根据顺序字典中的值对每个列表进行排序


在脚本的一次执行期间,所有列表可能如下所示

order = {
    'file_name':    0,
    'item_name':    1, 
    'item_height':  2,
    'item_width':   3,
    'item_depth':   4,
    'texture_file': 5
}
['thisfile.flt', 'box', '8.7', '10.5', '2.2']
['thatfile.flt', 'teapot', '6.0', '12.4', '3.0']
['thisfile.flt', 'box', '8.7', '10.5', 'red.jpg']
['thatfile.flt', 'teapot', '6.0', '12.4', 'blue.jpg']
另一方面,它们可能看起来像这样

order = {
    'file_name':    0,
    'item_name':    1, 
    'item_height':  2,
    'item_width':   3,
    'item_depth':   4,
    'texture_file': 5
}
['thisfile.flt', 'box', '8.7', '10.5', '2.2']
['thatfile.flt', 'teapot', '6.0', '12.4', '3.0']
['thisfile.flt', 'box', '8.7', '10.5', 'red.jpg']
['thatfile.flt', 'teapot', '6.0', '12.4', 'blue.jpg']
我想我的问题是,我该如何从一个字典中的特定值中创建一个列表,并根据另一个字典中的值进行排序,该字典与第一个字典具有相同的键


感谢您的任何想法/建议,很抱歉我的行为不端-我仍在学习python/编程

第一个代码框有无效的python语法(我怀疑
d=
部分是无关的…?)以及不明智地践踏内置名称
列表

无论如何,举个例子:

d = {'file_name':'thisfile.flt', 'item_name':'box', 'item_height':'8.7', 
     'item_width':'10.5', 'item_depth':'2.2', 'texture_file': 'red.jpg'}

order = {
    'file_name':    0,
    'item_name':    1, 
    'item_height':  2,
    'item_width':   3,
    'item_depth':   4,
    'texture_file': 5
}
获得所需结果的一个好方法是['thisfile.flt','box','8.7','10.5','2.2','red.jpg']:

def doit(d, order):
  return  [d[k] for k in sorted(order, key=order.get)]

啊,是的,我在发帖之前对帖子重新编辑了太多次,结果变得草率。感谢你的简洁回答,我从来没有想到这不是我在2.6.2中得到的:>>>排序((d[k]表示按顺序排列的k),key=order.get,reverse=True)['8.7','box',thisfile.flt',red.jpg',2.2',10.5']在3.0中,我得到了一个错误:>>>排序((d[k]表示按顺序排列的k),key=order.get,reverse=True Traceback(上次调用):File“”,第1行,in-TypeError:unorderable types:NoneType()>>>>导入操作符>>[d[k]表示k,按排序(order.items(),key=operator.itemgetter(1))]['thisfile.flt',box',8.7',10.5',2.2',red.jpg']@hugh,tx为了发现这个bug,我现在就编辑修复了它——但是您上一个代码片段中的代码没有运行(您正在用字符串键寻址一个列表,所以它当然不能运行).是的:在python提示符下运行命令的结果末尾的内容。当我发布它时,它看起来很好。是的,能够在注释中格式化代码会使生活变得更简单。
namedtuple
类可能比这里的字典更适合您的用途。它在python 2.6+上的
集合
模块中,或者如果您使用的是2.4或2.5,请从Python食谱中获取: