例如,这是我的词典列表:
[{'name': 'John', 'color': 'red' },
{'name': 'Bob', 'color': 'green'},
{'name': 'Tom', 'color': 'blue' }]
根据列表['blue', 'red', 'green']
,我想返回以下内容:
[{'name': 'Tom', 'color': 'blue' },
{'name': 'John', 'color': 'red' },
{'name': 'Bob', 'color': 'green'}]
答案 0 :(得分:4)
这可能有点天真,但它有效:
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
答案 1 :(得分:2)
更新
>>> 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}]
答案 2 :(得分:1)
您可以sort使用任何自定义键功能。
>>> 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'}]
答案 3 :(得分:1)
重复哈托的解决方案:
>>> 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'}]
>>>
答案 4 :(得分:0)
这是一个简单的循环函数:
# 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)))
即使对于非常大的列表,这也会非常快。
答案 5 :(得分:0)
假设:
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]
如果您要使用相同的词典列表但不同的颜色列表多次执行此操作,您需要将索引构建拆分并保留索引,这样您就不需要每次都重建它