本文介绍了Python:列表列表的唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am curious what would be an efficient 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. The return value should be a list of lists as same as the testdata, but with only the unique values kept.

推荐答案

您可以使用一组:

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

您还可以查看 此页面,其中对各种方法进行了基准测试,这些方法要么保留或不保留顺序.

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

这篇关于Python:列表列表的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:29