Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://docs.python.org/2/library/itertools.html#itertools.groupby" rel="nofollow"><code>itertools.groupby</code></a> with some additional processing of what it outputs will do the job:</p> <pre><code>from itertools import groupby data = { ('9', '0000022', 'LRA'), ('45', '0000016', 'PBM'), ('16', '0000048', 'PBL'), ... } def group_by_name_and_id(s): grouped = groupby(sorted(s), key=lambda (name, id_, type_): (name_, id)) for (name, id_), items in grouped: types = tuple(type_ for _, _, type_ in items) if len(types) &gt; 1: yield (name, id_) + types print '\n'.join(str(x) for x in group_by_name_and_id(data)) </code></pre> <p>outputs:</p> <pre><code>('10', '0000007', 'PBM', 'IBL') ('12', '0000051', 'LRA', 'PBL') ('7', '0000014', 'LRA', 'PBL', 'IBL') ('9', '0000022', 'LRA', 'PBL', 'IBL') </code></pre> <p><strong>P.S.</strong> but I don't really like that design: thet types could/should really be a list contained in the 3rd item of the tuple, not part of the tuple itself... because this way the tuple is dynamic in length, and that's ugly... tuples aren't meant to be used like that. So best to replace</p> <pre><code> types = tuple(type_ for _, _, type_ in items) yield (name, id_) + types </code></pre> <p>with</p> <pre><code> types = [type_ for _, _, type_ in items] yield (name, id_, types) </code></pre> <p>yielding the much cleaner looking</p> <pre><code>('10', '0000007', ['IBL', 'PBM']) ('12', '0000051', ['LRA', 'PBL']) ('7', '0000014', ['IBL', 'LRA', 'PBL']) ('9', '0000022', ['IBL', 'LRA', 'PBL']) </code></pre> <p>for example then you can just iterate over the resulting data with <code>for name, id, types in transformed_data:</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload