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


Groups > comp.lang.python > #110325

Re: Summary grid

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From jmp <jeanmichel@sequans.com>
Newsgroups comp.lang.python
Subject Re: Summary grid
Date Wed, 22 Jun 2016 18:20:37 +0200
Lines 56
Message-ID <mailman.44.1466612453.11516.python-list@python.org> (permalink)
References <CACvW2fz29iNOxe0E0ZrqZU7=ov6s0VZbB_2bLFG37WqgOZEPrw@mail.gmail.com> <nkedsl$pl3$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de A/gUu92wll0pAXRF2EwF/Ajqr+KRT5MptzQkL7+Gd7Ng==
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; 'data:': 0.07; '[1,': 0.09; 'builtins': 0.09; 'grid': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'python': 0.10; '"d",': 0.16; '"zip"': 0.16; '0],': 0.16; '[2,': 0.16; 'categories,': 0.16; 'duplicates': 0.16; 'magic': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:Summary': 0.16; 'wrote:': 0.16; 'function:': 0.22; 'cheers,': 0.22; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X -Complaints-To:1': 0.26; 'entered': 0.27; 'function': 0.28; 'cat': 0.29; 'summary': 0.29; 'print': 0.30; 'code': 0.30; 'this?': 0.34; 'list': 0.34; 'could': 0.35; 'done': 0.35; 'item': 0.35; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'say': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'skip:z 10': 0.38; 'data': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'care': 0.60; 'received:194': 0.61; 'charset:windows-1252': 0.62; 'results': 0.66; 'worth': 0.67
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host paris.sequans.com
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
In-Reply-To <CACvW2fz29iNOxe0E0ZrqZU7=ov6s0VZbB_2bLFG37WqgOZEPrw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <nkedsl$pl3$1@ger.gmane.org>
X-Mailman-Original-References <CACvW2fz29iNOxe0E0ZrqZU7=ov6s0VZbB_2bLFG37WqgOZEPrw@mail.gmail.com>
Xref csiph.com comp.lang.python:110325

Show key headers only | View raw


On 06/22/2016 04:46 PM, Jignesh Sutar wrote:
> Say I have list of data as given in the example code below, I want to find
> all the unique categories (alphabetic letters) and unique IDs (numbers) and
> then produce a summary grid as manually entered in the "results". How could
> I code this?
>
> Many thanks in advance,
> Jignesh
>
>
> data= ["A.1", "A.2", "A.3", "B.1", "C.2", "C.3",  "D.4", "E.5", "E.6"]
>
> cols=[]
> rows=[]
> for item in data:
>      i=item.split(".")
>      if i[0] not in cols: cols.append(i[0])
>      if i[1] not in rows: rows.append(i[1])
>
> print cols
> print rows
>
> results=
> [["Row/Col", "A", "B", "C", "D", "E"],
> [1, 1, 1, 0, 0, 0],
> [2, 1, 0, 1, 0, 0],
> [3, 1, 0, 1, 0, 0],
> [4, 0, 0, 0, 1, 0],
> [5, 0, 0, 0, 0, 1],
> [6, 0, 0, 0, 0, 1]]
>

Easily done using only builtins and list comprehension:

data= ["A.1", "A.2", "A.3", "B.1", "C.2", "C.3",  "D.4", "E.5", "E.6"]
# python magic with the "zip" function
# "set" will take care of removing duplicates
categories, ids = map(set, zip(*[d.split('.') for d in data]))

results = []
for id_ in sorted(map(int,ids)):
     results.append([data.count("%s.%d" % (cat, id_)) for cat in 
sorted(categories)])

print results



If you don't really understand the zip function:
http://nedbatchelder.com/text/iter.html
The whole presentation is worth reading.

Cheers,

Jm

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


Thread

Re: Summary grid jmp <jeanmichel@sequans.com> - 2016-06-22 18:20 +0200

csiph-web