返回与python中相应的值列表相匹配的词典列表

返回与python中相应的值列表相匹配的词典列表,python,dictionary,Python,Dictionary,例如,这是我的字典列表: [{'name': 'John', 'color': 'red' }, {'name': 'Bob', 'color': 'green'}, {'name': 'Tom', 'color': 'blue' }] 根据列表,我想返回以下内容: [{'name': 'Tom', 'color': 'blue' }, {'name': 'John', 'color': 'red' }, {'name': 'Bob', 'color': 'green'}

例如,这是我的字典列表:

[{'name': 'John', 'color': 'red'  },
 {'name': 'Bob',  'color': 'green'},
 {'name': 'Tom',  'color': 'blue' }] 
根据列表,我想返回以下内容:

[{'name': 'Tom',  'color': 'blue' },
 {'name': 'John', 'color': 'red'  },
 {'name': 'Bob',  'color': 'green'}]
更新:

>>> list_ = [{'c': 3}, {'c': 2}, {'c': 5}]
>>> mp = [3, 5, 2]
>>> sorted(list_, cmp=lambda x, y: cmp(mp.index(x.get('c')), mp.index(y.get('c'))))
[{'c': 3}, {'c': 5}, {'c': 2}]

下面是一个简单的循环函数:

# Heres the people:
people = [{'name':'John', 'color':'red'}, 
          {'name':'Bob', 'color':'green'}, 
          {'name':'Tom', 'color':'blue'}] 

# Now we can make a method to get people out in order by color:
def orderpeople(order):
    for color in order:
        for person in people:
            if person['color'] == color:
                yield person

order = ['blue', 'red', 'green']
print(list(orderpeople(order)))
如果你有很多人的话,这将是非常缓慢的。然后,您只能循环浏览它们一次,但要按颜色建立索引:

# Here's the people:
people = [{'name':'John', 'color':'red'}, 
          {'name':'Bob', 'color':'green'}, 
          {'name':'Tom', 'color':'blue'}] 

# Now make an index:
colorindex = {}
for each in people:
    color = each['color']
    if color not in colorindex:
        # Note that we want a list here, if several people have the same color.
        colorindex[color] = [] 
    colorindex[color].append(each)

# Now we can make a method to get people out in order by color:
def orderpeople(order):
    for color in order:
        for each in colorindex[color]:
            yield each

order = ['blue', 'red', 'green']
print(list(orderpeople(order)))
即使是非常大的列表,这也会非常快。

给定:

people = [{'name':'John', 'color':'red'}, {'name':'Bob', 'color':'green'}, {'name':'Tom', 'color':'blue'}]
colors = ['blue', 'red', 'green']
您可以这样做:

def people_by_color(people, colors):
  index = {}
  for person in people:
    if person.has_key('color'):
      index[person['color']] = person       
  return [index.get(color) for color in colors]

如果要多次使用相同的词典列表但不同的颜色列表执行此操作,则需要拆分索引构建并保留索引,这样您就不必每次都重新构建它。

这可能有点奇怪,但它是有效的:

data = [
    {'name':'John', 'color':'red'},
    {'name':'Bob', 'color':'green'},
    {'name':'Tom', 'color':'blue'}
]
colors = ['blue', 'red', 'green']
result = []

for c in colors:
    result.extend([d for d in data if d['color'] == c])

print result
您可以使用任何自定义键函数

>>> people = [
    {'name': 'John', 'color': 'red'},
    {'name': 'Bob', 'color': 'green'},
    {'name': 'Tom', 'color': 'blue'},
]
>>> colors = ['blue', 'red', 'green']
>>> sorted(people, key=lambda person: colors.index(person['color']))
[{'color': 'blue', 'name': 'Tom'}, {'color': 'red', 'name': 'John'}, {'color': 'green', 'name': 'Bob'}]
但是,list.index需要线性时间,因此如果颜色的数量可以增加,则转换为更快的键查找

>>> colorkeys = dict((color, index) for index, color in enumerate(colors))
>>> sorted(people, key=lambda person: colorkeys[person['color']])
[{'color': 'blue', 'name': 'Tom'}, {'color': 'red', 'name': 'John'}, {'color': 'green', 'name': 'Bob'}]

在Harto的解决方案上进行Riffing:

>>> from pprint import pprint
>>> [{'color': 'red', 'name': 'John'},
...  {'color': 'green', 'name': 'Bob'},
...  {'color': 'blue', 'name': 'Tom'}]
[{'color': 'red', 'name': 'John'}, {'color': 'green', 'name': 'Bob'}, {'color':
'blue', 'name': 'Tom'}]
>>> data = [
...     {'name':'John', 'color':'red'},
...     {'name':'Bob', 'color':'green'},
...     {'name':'Tom', 'color':'blue'}
... ]
>>> colors = ['blue', 'red', 'green']
>>> result = [d for d in data for c in colors if d['color'] == c]
>>> pprint(result)
[{'color': 'red', 'name': 'John'},
 {'color': 'green', 'name': 'Bob'},
 {'color': 'blue', 'name': 'Tom'}]
>>>
主要区别在于使用列表理解来构建结果

编辑:我在想什么?这显然需要使用any()表达式:

>>> from pprint import pprint
>>> data = [{'name':'John', 'color':'red'}, {'name':'Bob', 'color':'green'}, {'name':'Tom', 'color':'blue'}]
>>> colors = ['blue', 'red', 'green']
>>> result = [d for d in data if any(d['color'] == c for c in colors)]
>>> pprint(result)
[{'color': 'red', 'name': 'John'},
 {'color': 'green', 'name': 'Bob'},
 {'color': 'blue', 'name': 'Tom'}]
>>>

我会把它们按蓝色、绿色、红色的顺序排列,这不是要求的。听起来像是家庭作业。如果是这样的话,你应该把它标记出来。这不是家庭作业。为了更好地理解,我只是尽可能地简化了列表。@teggy-我编辑了你的问题,使它看起来更好。我希望你不介意。如果您不同意我所做的任何更改,或者只是想了解如何在StackOverflow上格式化代码,您可以自己编辑它。@teggy:好的。请注意,简化案例可能会得到无用的答案。:)例如,您拥有与字典列表相同的长度排序列表,并且没有重复的颜色。如果不是这样,下面的一些答案将不起作用。别忘了选择答案。值得注意的是,此解决方案处理的情况是人员列表大于颜色列表,以及多人具有相同颜色。如果这是一个家庭作业问题(我怀疑),那么这可能是没有必要的。但是,如果这不是一个家庭作业问题,也不是一个关于通用用例的问题,那么这将处理这些通用用例。我会说简单而直接,而不是幼稚的+1:key=方法本质上比cmp=one快(谢天谢地,在Py3中删除了!)而colorkeys的构建是一个有价值的优化——所以基本上我没有任何东西可以在单独的答案中添加(做得好!-)。酷。我不确定这种嵌套列表理解是否存在!