2015-02-24 91 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'}]