Python:列表列表的唯一性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3724551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:25:50  来源:igfitidea点击:

Python: Uniqueness for list of lists

pythonunique

提问by Hellnar

I am curious what would be an effient way of uniquefying such data objects:

我很好奇什么是唯一化此类数据对象的有效方法:

testdata =[ ['9034968', 'ETH'], ['14160113', 'ETH'], ['9034968', 'ETH'], ['11111', 'NOT'], ['9555269', 'NOT'], ['15724032', 'ETH'], ['15481740', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'], ['10307528', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'], ['15481740', 'ETH'], ['15379365', 'ETH'], ['11111', 'NOT'], ['9555269', 'NOT'], ['15379365', 'ETH']
]

For each data pair, left numeric string PLUS the type at the right tells the uniqueness of a data element. And it returns a list of lists as same as the testdata, but only uniques are existing.

对于每个数据对,左边的数字字符串加上右边的类型说明数据元素的唯一性。它返回一个与测试数据相同的列表列表,但只有唯一的存在。

Regards

问候

采纳答案by Mark Byers

You can use a set:

您可以使用一组:

unique_data = [list(x) for x in set(tuple(x) for x in testdata)]

You can also see this pagewhich benchmarks a variety of methods that either preserve or don't preserve order.

您还可以查看此页面该页面对保留或不保留顺序的各种方法进行了基准测试。

回答by Manoj Govindan

I tried @Mark's answer and got an error. Converting the list and each elements into a tuple made it work. Not sure if this the best way though.

我尝试了@Mark 的回答,但出现错误。将列表和每个元素转换为元组使其工作。不确定这是否是最好的方法。

list(map(list, set(map(lambda i: tuple(i), testdata))))

Of course the same thing can be expressed using a list comprehension instead.

当然,同样的事情可以用列表推导来表达。

[list(i) for i in set(tuple(i) for i in testdata)]

I am using Python 2.6.2.

我正在使用 Python 2.6.2。

Update

更新

@Mark has since changed his answer. His current answer uses tuples and will work. So will mine :)

@Mark 从那以后改变了他的答案。他目前的答案使用元组并且会起作用。我的也会这样:)

Update 2

更新 2

Thanks to @Mark. I have changed my answer to return a list of lists rather than a list of tuples.

感谢@Mark。我已更改答案以返回列表列表而不是元组列表。

回答by pyfunc

import sets
testdata =[ ['9034968', 'ETH'], ['14160113', 'ETH'], ['9034968', 'ETH'], ['11111', 'NOT'], ['9555269', 'NOT'], ['15724032', 'ETH'], ['15481740', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'], ['10307528', 'ETH'], ['15481757', 'ETH'], ['15481724', 'ETH'], ['15481740', 'ETH'], ['15379365', 'ETH'], ['11111', 'NOT'], ['9555269', 'NOT'], ['15379365', 'ETH']]
conacatData = [x[0] + x[1] for x in testdata]
print conacatData
uniqueSet = sets.Set(conacatData)
uniqueList = [ [t[0:-3], t[-3:]] for t in uniqueSet]
print uniqueList

回答by Sam Morgan

Expanding a bit on @Mark Byerssolution, you can also just do one list comprehension and typecast to get what you need:

@Mark Byers解决方案进行一些扩展,您也可以只做一个列表理解和类型转换来获得您需要的内容:

testdata = list(set(tuple(x) for x in testdata))

Also, if you don't like list comprehensions as many find them confusing, you can do the same in a for loop:

此外,如果您不喜欢列表推导式,因为很多人认为它们令人困惑,您可以在 for 循环中执行相同的操作:

for i, e in enumerate(testdata):
    testdata[i] = tuple(e)
testdata = list(set(testdata))

回答by Khan

if you have a list of objects than you can modify @Mark Byers answer to:

如果您有一个对象列表,您可以将@Mark Byers 的答案修改为:

unique_data = [list(x) for x in set(tuple(x.testList) for x in testdata)]

where testdata is a list of objects which has a list testList as attribute.

其中 testdata 是具有列表 testList 作为属性的对象列表。