I'm trying to efficientlly group / nest a list of vertices by their neighbourhood size, into a list of lists of vertices.
The neighbourhood size is a property of a vertex v can be obtained by calling len(v.neighbours).
The input I have is an unsorted list of vertices. The output I'm trying to obtain should look like this:
[[all vertices with len(v.neighbours) == 1], [... == 2], [... == 4]]
It should be a list of lists where each sublist contains vertices with the same neighbourhood size, sorted from small to big, without empty lists. I don't need the indices of the sublists to map to the neighbourhood size of the contained vertices.
I know how to achieve this with list comprehension, but it's rather inefficient:
def _group(V: List[Vertex], max: int) -> List[List[Vertex]]:
return [[v for v in V if v.label == i] for i in range(max)]
Additionally, I don't want to pass the maximum neighbourhood size as a parameter, but calculate that during the grouping, and I'm looking for a way to filter out empty lists during the grouping as well.
I've looked into more efficient ways to group the vertices, for example by using a dictionary as intermediary step, but I haven't managed to produce a working result.
Could anyone tell me the most efficient way to group / nest the list of vertices?
Thanks in advance, and sorry if this has been posted before, but I couldn't find what I was looking for in another question.