从列表列表中获取唯一项目?

Get unique items from list of lists?(从列表列表中获取唯一项目?)
本文介绍了从列表列表中获取唯一项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的列表:

I have a list of lists that looks like this:

animal_groups = [['fox','monkey', 'zebra'], ['snake','elephant', 'donkey'],['beetle', 'mole', 'mouse'],['fox','monkey', 'zebra']]

删除重复列表的最佳方法是什么?使用上面的例子,我正在寻找可以产生这个的代码:

What is the best to remove duplicate lists? Using the above example, I am looking for code that would produce this:

uniq_animal_groups = [['fox','monkey', 'zebra'], ['snake','elephant', 'donkey'],['beetle', 'mole', 'mouse']]

我最初认为我可以使用 set(),但这似乎不适用于列表列表.我还看到了一个使用 itertools 的示例,但代码对我来说并不完全清楚.感谢您的帮助!

I first thought I could use set(), but this doesn't appear to work on a list of lists. I also saw an example using itertools, but the code was not entirely clear to me. Thanks for the help!

推荐答案

uniq_animal_groups = set(map(tuple, animal_groups))

可以解决问题,尽管您最终会得到一组元组而不是一组列表.(当然您可以将其转换回列表列表,但除非有特定原因,否则为什么要这样做?)

will do the trick, though you will end up with a set of tuples instead of a set of lists. (Of course you could convert this back to a list of lists, but unless there is a specific reason to do so, why bother?)

这篇关于从列表列表中获取唯一项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

【网站声明】本站部分内容来源于互联网,旨在帮助大家更快的解决问题,如果有图片或者内容侵犯了您的权益,请联系我们删除处理,感谢您的支持!

相关文档推荐

How to draw a rectangle around a region of interest in python(如何在python中的感兴趣区域周围绘制一个矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 检测和跟踪人员?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在图像的多个矩形边界框中应用阈值?)
How can I download a specific part of Coco Dataset?(如何下载 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根据文本方向检测图像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 检测图像中矩形的中心和角度)