python列表嵌套取值_从python的嵌套列表中获取唯一值

I have a nested list (list of list) and I want to remove the duplicates but I'm getting an error. This is an example:

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"

}

]

]

So at the final this images will only contains

[

[

{

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

}

]

]

I'm using the set function

set.__doc__

'set() -> new empty set object\nset(iterable) -> new set object\n\nBuild an unor

dered collection of unique elements.'

my trace log:

list(set(images))

Traceback (most recent call last):

File "", line 1, in

TypeError: unhashable type: 'list'

To make it simpler how can I remove all the duplicate in this example

example = [ [{'a':1, 'b':2}, 'w', 2], [{'a':1, 'b':2}, 'w', 2] ]

#result

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

解决方案

The set and dict containers rely on hashing of data. Other mutable containers like list (and the set and dict themselves) cannot be hashed. They may be changed later on (mutable), so a constant hash value makes no sense.

But you could transform all your data to (nested) tuples and finally into a set. Since tuple is an immutable container - and your data is hashable (strings) - it can work. Here's a nasty one-liner for your special images case that does the trick:

images_Set = set([tuple([tuple(sorted(image_dict.items()))

for image_dict in inner_list]) for inner_list in images])

and

print(images_set)

prints

{((('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')))}

EDIT: There's no guaranteed order for the items function of dictionaries. Hence, I also added sorted to ensure an order.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值