2015-02-24 94 views
0

我有一個嵌套列表(列表的列表),我想刪除重複,但我得到一個錯誤。這是一個例子:從python嵌套列表中獲取唯一值

images = [ 
    [ 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "marine-transportation-transports-maritimes.xml" 
     }, 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "railway-transportation-transports-ferroviaires.xml" 
     } 
    ], 
    [ 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "marine-transportation-transports-maritimes.xml" 
     }, 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "railway-transportation-transports-ferroviaires.xml" 
     } 
    ], 
    [ 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "marine-transportation-transports-maritimes.xml" 
     }, 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "railway-transportation-transports-ferroviaires.xml" 
     } 
    ] 
] 
在最後

所以這images將只包含

[ 
    [ 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "marine-transportation-transports-maritimes.xml" 
     }, 
     { 
      "image_link": "1969.1523.001.aa.cs.jpg", 
      "catalogue_number": "1969.1523", 
      "dataset_name": "railway-transportation-transports-ferroviaires.xml" 
     } 
    ] 
] 

我使用的set功能

set.__doc__ 
'set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unor 
dered collection of unique elements.' 

我跟蹤日誌:

list(set(images)) 
Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
TypeError: unhashable type: 'list' 

,使其更簡單我怎麼能刪除所有重複在這個例子中

example = [ [{'a':1, 'b':2}, 'w', 2], [{'a':1, 'b':2}, 'w', 2] ] 
#result 
#example = [[{'a':1, 'b':2}, 'w', 2] ] 
+0

unhashable類型:「名單」 這意味着它不能哈希列表,因爲列表是可變的,你不能哈希一個可變對象,如果你的數據是靜態的,你可以改變列表的元組。 – danielfranca 2015-02-24 11:43:08

+0

與字典的問題是,即使您將它們變爲元組(不可變),您也沒有任何方法來定義元素順序,這會破壞比較。 一個好的解決方案將涉及將這個數據結構轉換爲不可變的可排序數據結構,然後清除重複項(例如使用'set')。 – Lachezar 2015-02-24 11:53:44

回答

1

setdict容器依賴於數據的哈希。其他可變容器,如list(和setdict本身)不能被散列。它們可能稍後會改變(可變),所以恆定的散列值是沒有意義的。

但是,您可以將所有數據轉換爲(嵌套)元組,最後轉換爲set。由於tuple是一個不可變的容器 - 並且您的數據可哈希(字符串) - 它可以工作。這裏是一個討厭的一個班輪爲您的特殊的圖像,做的伎倆情況:

images_Set = set([tuple([tuple(sorted(image_dict.items())) 
    for image_dict in inner_list]) for inner_list in images]) 

print(images_set) 

打印

{((('catalogue_number', '1969.1523'), 
    ('dataset_name', 'marine-transportation-transports-maritimes.xml'), 
    ('image_link', '1969.1523.001.aa.cs.jpg')), 
    (('catalogue_number', '1969.1523'), 
    ('dataset_name', 'railway-transportation-transports-ferroviaires.xml'), 
    ('image_link', '1969.1523.001.aa.cs.jpg')))} 

編輯:有沒有保證的順序爲詞典的功能items。因此,我還添加了sorted以確保訂單。

+0

當您將字典翻譯爲圖形時,元素的順序如何? CPython實現細節:鍵和值以非隨機的任意順序列出,在Python實現中有所不同,並且取決於字典的插入和刪除歷史。「(https://docs.python .org/2/library/stdtypes.html#dict.items) – Lachezar 2015-02-24 12:00:31

+0

你說得對,我加入排序 – SmCaterpillar 2015-02-24 12:01:49

+0

謝謝@SmCaterpillar的解釋。感謝Kasra,Avinash,Lucho你們都有很好的回答。 – 2015-02-24 12:10:25

1

好像你想是這樣的,

>>> example = [ [{'a':1, 'b':2}, 'w', 2], [{'a':1, 'b':2}, 'w', 2] ] 
>>> l = [] 
>>> for i in example: 
     if i not in l: 
      l.append(i) 


>>> l 
[[{'b': 2, 'a': 1}, 'w', 2]] 
+0

謝謝@Avinash!我猜這裏沒有內置函數! – 2015-02-24 11:48:19

1

您可以使用compiler.ast.flatten扁平化你的清單,然後將其轉換你的字典的哈希的對象grub的集合再轉換回字典,只是用一個列表理解:

>>> from compiler.ast import flatten 
>>> [dict(item) for item in set(tuple(i.items()) for i in flatten(images))] 
[{'image_link': '1969.1523.001.aa.cs.jpg', 'catalogue_number': '1969.1523', 'dataset_name': 'marine-transportation-transports-maritimes.xml'}, {'image_link': '1969.1523.001.aa.cs.jpg', 'catalogue_number': '1969.1523', 'dataset_name': 'railway-transportation-transports-ferroviaires.xml'}]