简体   繁体   English

根据另一个列表的值顺序对字典列表进行排序

[英]Sorting a list of dictionaries based on the order of values of another list

I'm using python 2.7.3, and I'm trying to sort a list of dictionaries based on the order of values of another list.我正在使用 python 2.7.3,我正在尝试根据另一个列表的值顺序对字典列表进行排序。

IE: IE:

listOne = ['hazel', 'blue', 'green', 'brown']
listTwo = [{'name': 'Steve', 'eyecolor': 'hazel', 'height': '5 ft. 11 inches'},
           {'name': 'Mark', 'eyecolor': 'brown', 'height': '6 ft. 2 inches'},
           {'name': 'Mike', 'eyecolor': 'blue', 'height': '6 ft. 0 inches'},
           {'name': 'Ryan', 'eyecolor': 'brown', 'height': '6 ft, 0 inches'},
           {'name': 'Amy', 'eyecolor': 'green', 'height': '5 ft, 6 inches'}]

Sorting listTwo based off of the order of values in listOne, we would end up with the following:根据 listOne 中值的顺序对 listTwo 进行排序,我们将得到以下结果:

print listTwo
[{'name': 'Steve', 'eyecolor': 'hazel', 'height': '5 ft. 11 inches'},
{'name': 'Mike', 'eyecolor': 'blue', 'height': '6 ft. 0 inches'},
{'name': 'Amy', 'eyecolor': 'green', 'height': '5 ft, 6 inches'},
{'name': 'Mark', 'eyecolor': 'brown', 'height': '6 ft. 2 inches'},
{'name': 'Ryan', 'eyecolor': 'brown', 'height': '6 ft, 0 inches'}]

I eventually need to output this text, so what I've done to display it correctly (in the correct order) is the following:我最终需要输出这个文本,所以我为正确显示它(以正确的顺序)所做的如下:

for x in xrange(len(listOne)):
    for y in xrange(len(listTwo)):
        if listOne[x] == listTwo[y]["eyecolor"]:
            print "Name: " + str(listTwo[y]["name"]),
            print "Eye Color: " + str(listTwo[y]["eyecolor"]),
            print "Height: " + str(listTwo[y]["height"])

Is there some sort of lambda expression that can be used to make this happen?是否有某种 lambda 表达式可用于实现这一目标? There has to be a more compact, less complex way of getting it in the order I want.必须有一种更紧凑、更简单的方式来按我想要的顺序获取它。

The simplest way would be to use list.index to generate a sort value for your list of dictionaries: 最简单的方法是使用list.index为您的词典列表生成排序值:

listTwo.sort(key=lambda x: listOne.index(x["eyecolor"]))

This is a little bit inefficient though, since list.index does a linear search through the eye-color list. 但这有点效率低,因为list.index通过眼睛颜色列表进行线性搜索。 If you had many eye colors to check against, it would be slow. 如果您有许多眼睛颜色要检查,那就会很慢。 A somewhat better approach would build an index dictionary instead: 一种更好的方法是构建一个索引字典:

order_dict = {color: index for index, color in enumerate(listOne)}
listTwo.sort(key=lambda x: order_dict[x["eyecolor"]])

If you don't want to modify listTwo , you can use the built-in sorted function instead of the list.sort method. 如果您不想修改listTwo ,则可以使用内置的sorted函数而不是list.sort方法。 It returns a sorted copy of the list, rather than sorting in-place. 它返回列表的排序副本,而不是就地排序。

listOne = ['hazel', 'blue', 'green', 'brown']
listTwo = [{'name': 'Steve', 'eyecolor': 'hazel', 'height': '5 ft. 11 inches'},{'name': 'Mark', 'eyecolor': 'brown', 'height': '6 ft. 2 inches'},{'name': 'Mike', 'eyecolor': 'blue', 'height': '6 ft. 0 inches'},{'name': 'Ryan', 'eyecolor': 'brown', 'height': '6 ft, 0 inches'},{'name': 'Amy', 'eyecolor': 'green', 'height': '5 ft, 6 inches'}]


order_list_dict = {color: index for index, color in enumerate(listOne)}


print(order_list_dict)

print(sorted(listTwo, key=lambda i: order_list_dict[i["eyecolor"]]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM