[英]Removing duplicates in list of lists
我有一个由列表组成的列表,每个子列表中都有4个项目(整数和浮点数)。 我的问题是我想删除那些index = 1和index = 3与其他子列表匹配的子列表。
[[1, 2, 0, 50], [2, 19, 0, 25], [3, 12, 25, 0], [4, 18, 50, 50], [6, 19, 50, 67.45618854993529], [7, 4, 50, 49.49657024231138], [8, 12, 50, 41.65340802385248], [9, 12, 50, 47.80600357035001], [10, 18, 50, 47.80600357035001], [11, 18, 50, 53.222014760339356], [12, 18, 50, 55.667812693447615], [13, 12, 50, 41.65340802385248], [14, 12, 50, 47.80600357035001], [15, 13, 50, 47.80600357035001], [16, 3, 50, 49.49657024231138], [17, 3, 50, 49.49657024231138], [18, 4, 50, 49.49657024231138], [19, 5, 50, 49.49657024231138]]
例如,[7、4、50、49.49657024231138]和[18、4、50、49.49657024231138]在索引1和3处具有相同的整数。因此,我想删除一个,而这无关紧要。
我看了一些代码,这些代码使我可以在单个索引的基础上执行此操作。
def unique_items(L):
found = set()
for item in L:
if item[1] not in found:
yield item
found.add(item[1])
我一直在使用此代码,该代码允许我删除列表,但仅基于单个索引即可删除(我还没有完全理解代码,但是它可以正常工作。)
因此,问题在于仅基于列表列表中index = 1和index = 3的重复值来删除子列表。
如果需要比较(item[1], item[3])
,请使用元组。 元组是可哈希化的类型,因此可以用作set成员或dict键。
def unique_items(L):
found = set()
for item in L:
key = (item[1], item[3]) # use tuple as key
if key not in found:
yield item
found.add(key)
这是使它起作用的方式:
def unique_items(L):
# Build a set to keep track of all the indices we've found so far
found = set()
for item in L:
# Now check if the 2nd and 4th index of the current item already are in the set
if (item[1], item[3]) not in found:
# if it's new, then add its 2nd and 4th index as a tuple to our set
found.add((item[1], item[3])
# and give back the current item
# (I find this order more logical, but it doesn't matter much)
yield item
这应该工作:
from pprint import pprint
d = {}
for sublist in lists:
k = str(sublist[1]) + ',' + str(sublist[3])
if k not in d:
d[k] = sublist
pprint(d.values())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.