Re: [Tutor] Lists of duplicates

2017-03-09 Thread Alex Kleider
On 2017-03-09 06:07, Steven D'Aprano wrote: On Wed, Mar 08, 2017 at 08:29:19PM -0800, Alex Kleider wrote: 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.setdefaul

Re: [Tutor] Lists of duplicates

2017-03-09 Thread Steven D'Aprano
On Wed, Mar 08, 2017 at 08:29:19PM -0800, Alex Kleider wrote: > 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] +=

Re: [Tutor] Lists of duplicates

2017-03-09 Thread Steven D'Aprano
On Wed, Mar 08, 2017 at 08:29:19PM -0800, Alex Kleider wrote: > 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

Re: [Tutor] Lists of duplicates

2017-03-09 Thread Steven D'Aprano
On Thu, Mar 09, 2017 at 01:26:26AM +0530, Sri Kavi wrote: > I wrote and submitted the following function: > > def sublist_duplicates(lst): > sublists = [] > for item in set(lst): > sublists.append([item] * lst.count(item)) > return sublists > > lst_of_duplicates = [1, 2, 10,

Re: [Tutor] Lists of duplicates

2017-03-09 Thread Mats Wichmann
On 03/08/2017 12:56 PM, Sri Kavi wrote: > As part of my learning, I was participating in a discussion at: > > > > https://discuss.codecademy.com/t/python-lists-of-duplicated-elements/78151 > > > > It’s about making a function that returns a list of lists, with each list > being all of the ele

Re: [Tutor] Lists of duplicates

2017-03-09 Thread Alan Gauld via Tutor
On 09/03/17 06:06, Alex Kleider wrote: > It seems you are simply kicking the can down the road rather than > explaining how it it is that dot notation actually works. The dot notation is just a method access. Dictionaries are like any other object and have many methods. > To access a dictionary

Re: [Tutor] Lists of duplicates

2017-03-09 Thread Alan Gauld via Tutor
On 09/03/17 04:29, Alex Kleider wrote: >> I'd probably opt for a dictionary: >> >> def f(lst): >> res = {} >> for item in lst: >> res.setdefault(item,[]).append(item) >> return list(res.values()) >> > The above works BUT > how setdefault returns the current value for the key

Re: [Tutor] Lists of duplicates

2017-03-09 Thread Peter Otten
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

Re: [Tutor] Lists of duplicates

2017-03-08 Thread Alex Kleider
On 2017-03-08 21:14, boB Stepp wrote: Alex, I think you can break this down as follows: py3: res = {} py3: def key_item_to_res(item): ... res.setdefault(item, []).append(item) ... py3: key_item_to_res(3) py3: res {3: [3]} py3: key_item_to_res(3) py3: res {3: [3, 3]} py3: key_item_to_res(2)

Re: [Tutor] Lists of duplicates

2017-03-08 Thread boB Stepp
On Wed, Mar 8, 2017 at 10:29 PM, Alex Kleider wrote: > On 2017-03-08 17:03, Alan Gauld via Tutor wrote: >> >> 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

Re: [Tutor] Lists of duplicates

2017-03-08 Thread Alex Kleider
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 prob

Re: [Tutor] Lists of duplicates

2017-03-08 Thread Alan Gauld via Tutor
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 differe

[Tutor] Lists of duplicates

2017-03-08 Thread Sri Kavi
As part of my learning, I was participating in a discussion at: https://discuss.codecademy.com/t/python-lists-of-duplicated-elements/78151 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 l