从列表列表中获取唯一的项目? [英] Get unique items from list of lists?
问题描述
我有一个这样的列表:
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?)
这篇关于从列表列表中获取唯一的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!