[英]Sort list of dictionaries by appearance of key in a second ordered list
我有兩個清單。 我的第一個列表first_list_ordered
包含字符串。
first_list_ordered = ["id1", "id2", "id3", "id4", "id5", "id6", "id7"]
我的第二個列表second_list_unsorted
包含字典,字典中至少有一個名為id
鍵,該值可能出現在first_list_ordered
。
second_list_unordered = [{"id": "id6", "content": "sth"},
{"id": "id4", "content": "sth"},
{"id": "id1", "content": "sth"},
{"id": "id3", "content": "sth"}]
現在,我想按第一張列表中id
值的出現順序對第二張列表進行排序。 結果應如下所示:
result = [{"id": "id1", "content": "sth"},
{"id": "id3", "content": "sth"},
{"id": "id4", "content": "sth"},
{"id": "id6", "content": "sth"}]
所以,如果你創建的所有值的列表id
在每一個字典的second_list_unordered
你得到的無序的子集中first_list_ordered
。
我的方法如下所示:
>>> first_list_ordered = ["id1", "id2", "id3", "id4", "id5", "id6", "id7"]
>>> second_list_unordered = [{"id": "id6", "content": "sth"}, {"id": "id4", "content": "sth"}, {"id": "id1", "content": "sth"}, {"id": "id3", "content": "sth"}]
>>> indices = {c: i for i, c in enumerate(first_list_ordered)}
>>> result = sorted(second_list_unordered, key=indices.get)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
顯然,這種方式是行不通的...現在我被卡住了。
感謝您的提示!
您需要將id
鍵傳遞給indices.get
,而不是整個字典:
result = sorted(second_list_unordered, key=lambda d: indices.get(d['id']))
演示:
>>> from pprint import pprint
>>> first_list_ordered = ["id1", "id2", "id3", "id4", "id5", "id6", "id7"]
>>> second_list_unordered = [{"id": "id6", "content": "sth"},
... {"id": "id4", "content": "sth"},
... {"id": "id1", "content": "sth"},
... {"id": "id3", "content": "sth"}]
>>> indices = {c: i for i, c in enumerate(first_list_ordered)}
>>> sorted(second_list_unordered, key=lambda d: indices.get(d['id']))
[{'content': 'sth', 'id': 'id1'}, {'content': 'sth', 'id': 'id3'}, {'content': 'sth', 'id': 'id4'}, {'content': 'sth', 'id': 'id6'}]
>>> pprint(_)
[{'content': 'sth', 'id': 'id1'},
{'content': 'sth', 'id': 'id3'},
{'content': 'sth', 'id': 'id4'},
{'content': 'sth', 'id': 'id6'}]
為了使它更加有趣,將first_list_ordered
改組,因為id
值的排序順序使目的有些模糊:
>>> import random
>>> random.shuffle(first_list_ordered)
>>> first_list_ordered
['id2', 'id7', 'id1', 'id4', 'id6', 'id5', 'id3']
>>> indices = {c: i for i, c in enumerate(first_list_ordered)}
>>> sorted(second_list_unordered, key=lambda d: indices.get(d['id']))
[{'content': 'sth', 'id': 'id1'}, {'content': 'sth', 'id': 'id4'}, {'content': 'sth', 'id': 'id6'}, {'content': 'sth', 'id': 'id3'}]
>>> pprint(_)
[{'content': 'sth', 'id': 'id1'},
{'content': 'sth', 'id': 'id4'},
{'content': 'sth', 'id': 'id6'},
{'content': 'sth', 'id': 'id3'}]
如果速度不是問題,那么為什么不手動進行呢?
這里便宜又臟的oneliner。
In [55]: second_list_unordered = [{"id": "id6", "content": "sth"}, {"id": "id4", "content": "sth"}, {"id": "id1", "content": "sth"}, {"id": "id3", "content": "sth"}]
In [56]: first_list_ordered = ["id1", "id2", "id3", "id4", "id5", "id6", "id7"]
In [57]: f = first_list_ordered
In [58]: s = second_list_unordered
In [59]: [oval[0] for oval in [[val for val in s if (val["id"] == key)] for key in f] if oval]
Out[59]:
[{'content': 'sth', 'id': 'id1'},
{'content': 'sth', 'id': 'id3'},
{'content': 'sth', 'id': 'id4'},
{'content': 'sth', 'id': 'id6'}]
In [60]: fff = ["id3", "id4", "id5", "id2", "id1", "id6", "id7"]
In [61]: [oval[0] for oval in [[val for val in s if (val["id"] == key)] for key in fff] if oval]
Out[61]:
[{'content': 'sth', 'id': 'id3'},
{'content': 'sth', 'id': 'id4'},
{'content': 'sth', 'id': 'id1'},
{'content': 'sth', 'id': 'id6'}]
相同,但為了理解而分為兩個操作:
In [62]: temp = [[val for val in s if (val["id"] == key)] for key in f]
In [63]: [val[0] for val in temp if val]
Out[63]:
[{'content': 'sth', 'id': 'id1'},
{'content': 'sth', 'id': 'id3'},
{'content': 'sth', 'id': 'id4'},
{'content': 'sth', 'id': 'id6'}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.