Better ways for implementing two situations

MRAB python at mrabarnett.plus.com
Sun Apr 21 15:10:51 EDT 2019


On 2019-04-21 19:23, Paulo da Silva wrote:
> 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.
> 
Have you compared the speed with an implementation that uses 
defaultdict? Your code always creates an empty list for each item, even 
though it might not be needed.


More information about the Python-list mailing list