[Tutor] Lists of duplicates

Alex Kleider akleider at sonic.net
Wed Mar 8 23:29:19 EST 2017


On 2017-03-08 17:03, Alan Gauld via Tutor wrote:
> On 08/03/17 19:56, Sri Kavi wrote:
> 
>> It’s about making a function that returns a list of lists, with each 
>> list
>> being all of the elements that are the same as another element in the
>> original list.
> 
> This is one of those problems where there is probably no
> definitive "correct" answer just different compromises.
> 
> My first reaction was to sort the initial list then iterate
> over it creating a new sublist for each change of value.
> But, it only works for homogenous lists. For mixed types
> I'd probably opt for a dictionary:
> 
> def f(lst):
>     res = {}
>     for item in lst:
> 	res.setdefault(item,[]).append(item)
>     return list(res.values())
> 
> But I've no idea how that performs compared to your methods.

The above works BUT
how????

Things like this can usually be broken down into their component parts 
but I've been unsuccessful doing so:

def f(lst):
     res = {}
     for item in lst:
         method_res = res.setdefault(item, [])
         res.method_res.append(item)
#       res.setdefault(item,[]).append(item)
     return list(res.values())
     res.method_res.append(item)

AttributeError: 'dict' object has no attribute 'method_res'

Python must be doing some trickery behind the scenes.

The algorithm I came up with is:
def make_sublists_of_duplicates(original_list):
     frequency_counter = {}
     for item in original_list:
         _ = frequency_counter.setdefault(item, 0)
         frequency_counter[item] += 1
     res = []
     for key in frequency_counter:
         res.append([key for i in range(frequency_counter[key])])
     return res

although I would argue that the creation of the frequency_counter should 
be in its own function and then it could be used to create the list of 
lists IF required. That would get around the criticism of not being able 
to deal with huge input.



More information about the Tutor mailing list