实施例:
list = [['1', '13/12/2016', [42, 52]], ['2', '12/12/2016', [36, 46]], ['4', '10/12/2016', [13, 23]], ['4', '10/12/2016', [42, 52]], ['5', '08/12/2016', [42, 52]], ['6', '07/12/2016', [32, 42]], ['7', '12/12/2016', [42, 52]], ['8', '06/12/2016', [42, 52]], ['10', '12/12/2016', [45, 55]], ['11', '08/12/2016', [42, 52]]]
我想检查每个子列表中的所有第二个元素是否重复 如果第二个元素在另一个子列表中重复,我想保留整个子列表
预期结果:
endlist = [['2', '12/12/2016', [36, 46]], ['4', '10/12/2016', [13, 23]], ['4', '10/12/2016', [42, 52]], ['5', '08/12/2016', [42, 52]], ['7', '12/12/2016', [42, 52]], ['10', '12/12/2016', [45, 55]], ['11', '08/12/2016', [42, 52]]]
(2016年8月12日| 10/12/2016 | 12/12/2016是双打)
我知道如何使用平面列表([x for x in l if l.count(x) > 1])
保留双打,但如何在包含子列表的列表中执行此操作?
答案 0 :(得分:2)
您可以使用列表推导:
lst = [['1', '13/12/2016', [42, 52]], ['2', '12/12/2016', [36, 46]], ['4', '10/12/2016', [13, 23]], ['4', '10/12/2016', [42, 52]], ['5', '08/12/2016', [42, 52]], ['6', '07/12/2016', [32, 42]], ['7', '12/12/2016', [42, 52]], ['8', '06/12/2016', [42, 52]], ['10', '12/12/2016', [45, 55]], ['11', '08/12/2016', [42, 52]]]
endlist = [sublist for sublist in lst if sum(x[1] == sublist[1] for x in lst) > 1]
# [['2', '12/12/2016', [36, 46]], ['4', '10/12/2016', [13, 23]], ['4', '10/12/2016', [42, 52]], ['5', '08/12/2016', [42, 52]], ['7', '12/12/2016', [42, 52]], ['10', '12/12/2016', [45, 55]], ['11', '08/12/2016', [42, 52]]]
答案 1 :(得分:1)
您可以将计数信息收集到字典中然后使用它。如果列表很大,这将很好地扩展:
myList = [['1', '13/12/2016', [42, 52]], ['2', '12/12/2016', [36, 46]], ['4', '10/12/2016', [13, 23]], ['4', '10/12/2016', [42, 52]], ['5', '08/12/2016', [42, 52]], ['6', '07/12/2016', [32, 42]], ['7', '12/12/2016', [42, 52]], ['8', '06/12/2016', [42, 52]], ['10', '12/12/2016', [45, 55]], ['11', '08/12/2016', [42, 52]]]
d = dict()
for subList in myList:
if subList[1] in d:
d[subList[1]] += 1
else:
d[subList[1]] = 1
doubles = [subList for subList in myList if d[subList[1]] >= 2]
如果您希望>=2
排除三元组等,您当然可以将==2
替换为doubles
。
编辑:如果要保留每个双重子列表的第一个匹配项,请修改字典,以便存储每个元素的索引。像这样:
d = dict()
for i,subList in enumerate(myList):
if subList[1] in d:
d[subList[1]].append(i)
else:
d[subList[1]] = [i]
firsts = [subList for i,subList in enumerate(myList) if len(d[subList[1]]) >= 2 and i == d[subList[1]][0]]
print(firsts) #prints [['2', '12/12/2016', [36, 46]], ['4', '10/12/2016', [13, 23]], ['5', '08/12/2016', [42, 52]]]
进一步编辑:以下是删除后续双打的解决方案:
d = dict()
for i,subList in enumerate(myList):
if not subList[1] in d:
d[subList[1]] = i #stores first index
noDoubles = [subList for i,subList in enumerate(myList) if i == d[subList[1]]]