Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38264
| X-Received | by 10.180.98.103 with SMTP id eh7mr644973wib.3.1360148590342; Wed, 06 Feb 2013 03:03:10 -0800 (PST) |
|---|---|
| X-Received | by 10.49.47.109 with SMTP id c13mr412658qen.3.1360148589889; Wed, 06 Feb 2013 03:03:09 -0800 (PST) |
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!feeder3.cambriumusenet.nl!feeder1.cambriumusenet.nl!feed.tweaknews.nl!209.85.212.215.MISMATCH!yu2no8996149wib.0!news-out.google.com!i11ni39862wiw.0!nntp.google.com!19no7817189wij.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Wed, 6 Feb 2013 03:03:09 -0800 (PST) |
| In-Reply-To | <bbac4565-5e6f-47a5-be8c-76622fb6b908@u31g2000pru.googlegroups.com> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=124.168.207.54; posting-account=ZAg6xAoAAAAmY8bBi3VzYjWntm8Ct1P8 |
| NNTP-Posting-Host | 124.168.207.54 |
| References | <bbac4565-5e6f-47a5-be8c-76622fb6b908@u31g2000pru.googlegroups.com> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| Message-ID | <df7cdfef-a9ca-440d-9090-301fdb17e692@googlegroups.com> (permalink) |
| Subject | Re: toy list processing problem: collect similar terms |
| From | Nick Mellor <thebalancepro@gmail.com> |
| Injection-Date | Wed, 06 Feb 2013 11:03:10 +0000 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| Xref | csiph.com comp.lang.python:38264 |
Show key headers only | View raw
You can just flatten the list using itertools and collate the results using defaultdict:
import itertools
from collections import defaultdict
sequence = ((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'))
# flatten the list to (0,'a','b',1,'c','d'...
chain = itertools.chain.from_iterable(sequence)
# use defaultdict to set up the final list and deal with initial empty dictionary key
final_list = defaultdict(list)
for i in chain:
if isinstance(i, int):
number = i
else:
final_list[number].append(i)
print (final_list.items())
HTH,
Nick
On Sunday, September 26, 2010 2:05:13 PM 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 | Find similar | Unroll thread
Re: toy list processing problem: collect similar terms Nick Mellor <thebalancepro@gmail.com> - 2013-02-06 03:03 -0800
csiph-web