Better ways for implementing two situations

Paulo da Silva p_s_d_a_s_i_l_v_a_ns at netcabo.pt
Sun Apr 21 14:23:22 EDT 2019


Hi all.

I am looking for improved solutions to these two problems.
They are to be in a program that deals with big data. So, they need to
be fast and save memory.

Problem 1.

I have a list of objects and want to split it in a list of groups.
Each group must have all "equal objects" and have more than one object.
"equal objects" is based on an id we can get from the object.
Nothing is sorted.

Here is my implementation:

splitter={}
for f in Objs:
	splitter.setdefault(f.getId1,[]).append(f)
groups=[gs for gs in splitter.values() if len(gs)>1]


Problem 2.

I know it seems confusing but pls. look at the implementation.
Here I have a list of lists of objects - groups.
Now let's say l is a given list from groups, containing objects.
I want to replace this list l with a new list of sublists where each
sublist is a list of "equal objects".
The new l must have more than one list.
The final result is a list of lists of lists.
Nothing is sorted.

Here is my implementation:

gs=[]
for g in groups:
	splitter={}
	for f in g:
		splitter.setdefault(f.getId2(),[]).append(f)
	gse=list(splitter.values())
	if len(gse)>1: gs.append(gse)

gs is the result.

Thanks for any comments.



More information about the Python-list mailing list