我有一个由列表组成的列表,每个子列表中有4个项目(整数和浮点数)。我的问题是我要删除索引为1且索引= 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的重复值删除子列表。
答案 0 :(得分:3)
如果您需要比较(item[1], item[3])
,请使用元组。元组是hashable类型,因此它可以用作集成员或字典键。
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)
答案 1 :(得分:1)
这就是你如何使它发挥作用:
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
答案 2 :(得分:0)
这应该有效:
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())