2014-09-30 49 views
0

我有兩個列表。我的第一個列表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"}] 

因此,如果您創建的所有值second_list_unordered每一個快譯通,你得到的first_list_ordered一個無序的子集中的id的列表。

我的辦法是這樣的:

>>> 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' 

顯然它不工作的方式......現在我卡住了。

感謝您的任何提示!

回答

1

您需要在通過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'}] 
+0

MERCI!按預期工作! :) – hampfes 2014-10-02 16:29:13

0

如果速度不是問題,那爲什麼不手動做呢?

這裏便宜又髒兮兮的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'}] 
+0

爲什麼手動有'sort()'函數爲你做它? – 2014-10-02 16:30:22

+0

有時它刷新手動做這些算法的事情:)。否則,是的,沒有理由。 – 2014-10-02 18:08:40

相關問題