Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81031

Re: How to group list of list with condition in Python

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:group': 0.05; 'subject:Python': 0.06; 'assign': 0.07; 'diff': 0.07; 'json': 0.07; '0.1': 0.09; 'below).': 0.09; 'key.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'steps:': 0.09; 'thrown': 0.09; 'subject:How': 0.10; '"-"': 0.16; '"-":': 0.16; "['a',": 0.16; "['f',": 0.16; 'collections': 0.16; 'defaultdict': 0.16; 'fits': 0.16; 'list)': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'threshold': 0.16; 'wrote:': 0.18; 'else,': 0.19; 'work,': 0.20; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'helper': 0.24; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'rest': 0.29; 'subject:list': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'fast.': 0.31; 'tuples': 0.31; 'lists': 0.32; 'another': 0.32; 'maybe': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'subject:with': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'next': 0.36; 'entry': 0.36; 'list': 0.37; 'step': 0.37; 'question,': 0.38; 'to:addr:python-list': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'according': 0.40; '2nd': 0.60; 'new': 0.61; 'first': 0.61; 'complete': 0.62; 'pick': 0.64; 'provide': 0.64; 'finish': 0.65; 'close': 0.67; 'lowest': 0.74; 'repeat': 0.74; 'introduce': 0.78; "'cluster'": 0.84; 'homework': 0.84; 'nicely.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How to group list of list with condition in Python
Date Wed, 19 Nov 2014 11:01:44 +0100
Organization None
References <CADVKSzyD7pSQg=r=DMfDWkMvGZyHbGmidaL9utaOa3ecLQWPzQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdb845.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15995.1416391327.18130.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1416391327 news.xs4all.nl 2944 [2001:888:2000:d::a6]:47153
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81031

Show key headers only | View raw


Gundala Viswanath wrote:

> I have the following list of lists that contains 6 entries:
> 
> lol = [['a', 3, 1.01], ['x',5, 1.00],['k',7, 2.02],['p',8, 3.00],
> ['b', 10, 1.09],
>        ['f', 12, 2.03]]
> 
> each list in lol contain 3 elements:
> 
> ['a', 3, 1.01]
>   e1  e2   e3
> 
> The list above is already sorted according to the e2 (i.e, 2nd element)
> 
> I'd like to 'cluster' the above list following roughly these steps:
> 
> 1. Pick the lowest entry (wrt. e2) in lol as the key of first cluster
> 2. Assign that as first member of the cluster (dictionary of list)
> 3. Calculate the difference current e3 in next list with first member
> of existing clusters.
> 3. If the difference is less than threshold, assign that list as the
> member of the corresponding cluster Else, create new cluster with
> current list as new key.
> 3. Repeat the rest until finish
> 
> The final result will look like this, with threshold <= 0.1.
> 
> dol = {'a':['a','x','b'], 'k':['k','f'], 'p':['p']}
> 
> I'm stuck with this step what's the right way to do it:
> 
> __BEGIN__
> import json
> from collections import defaultdict
> 
> thres = 0.1
> tmp_e3 = 0
> tmp_e1 = "-"
> 
> lol = [['a', 3, 1.01], ['x',5, 1.00],['k',7, 2.02],
>        ['p',8, 3.00], ['b', 10, 1.09], ['f', 12, 2.03]]
> 
> dol = defaultdict(list)
> for thelist in lol:
>     e1, e2, e3 = thelist
> 
>     if tmp_e1 == "-":
>         tmp_e1 = e1
>     else:
>         diff = abs(tmp_e3 - e3)
>         if diff > thres:
>             tmp_e1 = e1
> 
>     dol[tmp_e1].append(e1)
>     tmp_e1 = e1
>     tmp_e3 = e3
> 
> print json.dumps(dol, indent=4)
> __END__

I won't provide the complete solution for a homework question, but here's 
the idea that let me to working code:

Introduce a helper list (let's call it `pairs`) where you put (e1, e3) 
tuples (called `inner_e1`, `inner_e3` below).

Inside the loop over `lol` loop over `pairs` -- and if you find an item 
where the inner_e3 is close enough to e3 you stop and use the inner_e1 as 
the key: 

dol[inner_e1].append(e1)

If there is no inner_e3 close enough you add another item to pairs and a new 
key to dol:

pairs.append((e1, e3))
dol[e1] = [e1]

Hints: `for ... else` with a `break` thrown in fits the above description 
for the inner loop nicely.

Note that this 2-loop approach is not very efficient if the number of 
clusters is large -- but first make it work, then -- maybe -- make it fast.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How to group list of list with condition in Python Peter Otten <__peter__@web.de> - 2014-11-19 11:01 +0100

csiph-web