这是基于密度标准在图表中查找密集社区,下面的所有集合都是图表的密集社区
list L=[set([1]), set([2]), set([3]), set([4]), set([5]), set([6]), set([7]), set([8]), set([1, 2]), set([1, 3]), set([1, 4]), set([3, 4]), set([1, 3, 4]), set([1, 5]), set([2, 5]), set([4, 5]), set([1, 2, 5]), set([1, 4, 5]), set([2, 6]), set([4, 7]), set([8, 5]), set([8, 7])]
我需要获得此列表的最大集合,例如:在列表集([1,4,5])中是最大集合集([1]),集合([4]),集合([5]),集合([1,5]),集合( [1,4])并设置([4,5])所以我只需打印出设置([1,4,5])类似设置([1]),设置([3])。set([ 4]),设置([1,3]),设置([1,4]),设置([3,4])有一个最大设置集([1,3,4])所以我需要打印出来只设置([1,3,4]),你可以看到set([1,2,5])是一个最大集合,所有不是较大集合子集的集合被认为是最大集合所以我需要只获得不是较大集合的子集的最大集合
答案 0 :(得分:1)
L=[set([1]), set([2]), set([3]), set([4]), set([5]), set([6]), set([7]), set([8]), set([1, 2]), set([1, 3]), set([1, 4]), set([3, 4]), set([1, 3, 4]), set([1, 5]), set([2, 5]), set([4, 5]), set([1, 2, 5]), set([1, 4, 5]), set([2, 6]), set([4, 7]), set([8, 5]), set([8, 7])]
#this grouping the list by there length ...
values = set(map(lambda x:len(x), L))
newlist = {x:[y for y in L if len(y)==x] for x in values}
maxlen=max(values)
#checking for the subset.if no subset.it is returned
for i in values:
print [ key for key in newlist[i] if not any([ val.intersection(key)==key for j in values if j !=i for val in newlist[j] ])]
#output =[set([2, 6]), set([4, 7]), set([8, 5]), set([8, 7])] [set([1, 3, 4]), set([1, 2, 5]), set([1, 4, 5])]