Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38315
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-02-06 15:37 -0800 |
| References | <bbac4565-5e6f-47a5-be8c-76622fb6b908@u31g2000pru.googlegroups.com> <d99310f7-43e7-4b11-ad4d-67d5f4f53557@googlegroups.com> |
| Message-ID | <5a97e6cd-aa67-454a-8d95-6bd004224d03@googlegroups.com> (permalink) |
| Subject | Re: toy list processing problem: collect similar terms |
| From | Nick Mellor <thebalancepro@gmail.com> |
Oops! Not that sort stability is used in this algorithm. Was thinking of something else :-) N On Thursday, 7 February 2013 10:25:36 UTC+11, Nick Mellor wrote: > Python 3 version: > > > > from collections import defaultdict > > > > data = ((0,'a','b'),(1,'c','d'),(2,'e','f'),(3,'g','h'),(1,'i','j'),(2,'k','l'),(4,'m','n'),(2,'o','p'),(4,'q','r'),(5,'s','t')) > > > > register = defaultdict(list) > > for number, *letters in data: > > register[number].extend(letters) > > final = [] > > for i in sorted(register.keys()): > > final.append(register[i]) > > print (final) > > > > NB sorting is "stable" in Python, so sorted() will always keep the 1's, 2's etc in the same order as they were in the unsorted list. > > > > Nick > > > > On Sunday, 26 September 2010 14:05:13 UTC+10, Xah Lee wrote: > > > here's a interesting toy list processing problem. > > > > > > I have a list of lists, where each sublist is labelled by > > > a number. I need to collect together the contents of all sublists > > > sharing > > > the same label. So if I have the list > > > > > > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q > > > r) (5 s t)) > > > > > > where the first element of each sublist is the label, I need to > > > produce: > > > > > > output: > > > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t)) > > > > > > a Mathematica solution is here: > > > http://xahlee.org/UnixResource_dir/writ/notations_mma.html > > > > > > R5RS Scheme lisp solution: > > > http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_gmail.scm > > > by Sourav Mukherjee > > > > > > also, a Common Lisp solution can be found here: > > > http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1ded8824bc750b? > > > > > > anyone care to give a solution in Python, Perl, javascript, or other > > > lang? am guessing the scheme solution can be much improved... perhaps > > > using some lib but that seems to show scheme is pretty weak if the lib > > > is non-standard. > > > > > > Xah ∑ xahlee.org ☄
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: toy list processing problem: collect similar terms Nick Mellor <thebalancepro@gmail.com> - 2013-02-06 15:25 -0800 Re: toy list processing problem: collect similar terms Nick Mellor <thebalancepro@gmail.com> - 2013-02-06 15:37 -0800
csiph-web