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


Groups > comp.lang.python > #3259

Re: Pythonic infinite for loop?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Pythonic infinite for loop?
Followup-To comp.lang.python
Date Fri, 15 Apr 2011 11:32:31 +0200
Organization None
Lines 54
Message-ID <io93bg$lqm$1@solani.org> (permalink)
References <mailman.377.1302833455.9059.python-list@python.org> <io8vd5$8cq$1@solani.org> <mailman.386.1302856347.9059.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace solani.org 1302859952 22358 eJwFwYEBACAEBMCVhH81jsj+I3QH42KFE3QMhnf36X1EpxSJVMv1ONEiin1jPURJs92kRD8a7hDE (15 Apr 2011 09:32:32 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Fri, 15 Apr 2011 09:32:32 +0000 (UTC)
X-User-ID eJwFwQkBwDAIA0BLvCnIWRvwL2F36VC8E0hEbi4LlSpbwlAipL1d9E0dYSstVKSiO+/db949TtrMMmza8AMpvRUJ
Cancel-Lock sha1:HHxtVDXW2gqjR1Vy8+o1n73aRug=
X-NNTP-Posting-Host eJwFwQkBACAIBLBKIscXBwT6R3ATVtJnUFHIygK77JsG8a48CsWzs24ZOyCmugPnkSg+ncwWERKgKlx1InsCmmzvy1ofucEZFg==
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:3259

Followups directed to: comp.lang.python

Show key headers only | View raw


Chris Angelico wrote:

> On Fri, Apr 15, 2011 at 6:25 PM, Peter Otten <__peter__@web.de> wrote:
>> The initial data structure seems less than ideal. You might be able to
>> replace it with a dictionary like
>>
>> {"Keyword": [value_for_keyword_1, value_for_keyword_2, ...]}
>>
>> if you try hard enough.
> 
> The initial data structure comes from a CSV file, and is not under my
> control.
> 
> ChrisA

Here's some code that might give you an idea. You can ignore the chunk 
before 'import csv'; it is there to make the demo self-contained.

from contextlib import contextmanager

@contextmanager
def open(filename):
    assert filename == "example.csv"
    from StringIO import StringIO
    yield StringIO("""\
beta3,alpha1,alpha2,beta1,beta2
b31,a11,a21,b11,b21
b32,a12,a22,b12,b22
b33,a13,a23,b13,b23
b34,a14,a24,b14,b24
""")

import csv
import re

def parse_name(s):
    name, index = re.match(r"(.+?)(\d+)$", s).groups()
    return name, int(index)-1

with open("example.csv") as instream:
    rows = csv.reader(instream)
    header = next(rows)
    dct = {}
    appends = []
    for h in header:
        name, index = parse_name(h)
        outer = dct.setdefault(name, {})
        inner = outer.setdefault(index, [])
        appends.append(inner.append)
    for row in rows:
        for value, append in zip(row, appends):
            append(value)
print dct

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


Thread

Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 12:10 +1000
  Re: Pythonic infinite for loop? Nobody <nobody@nowhere.com> - 2011-04-15 03:33 +0100
  Re: Pythonic infinite for loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-15 02:33 +0000
    Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 13:58 +1000
      Re: Pythonic infinite for loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-15 12:52 +0000
        Re: Pythonic infinite for loop? Roy Smith <roy@panix.com> - 2011-04-15 09:13 -0400
        Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-16 01:35 +1000
          Re: Pythonic infinite for loop? Paul Rubin <no.email@nospam.invalid> - 2011-04-15 09:10 -0700
  Re: Pythonic infinite for loop? Paul Rubin <no.email@nospam.invalid> - 2011-04-15 00:24 -0700
    Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 17:35 +1000
      Re: Pythonic infinite for loop? Paul Rubin <no.email@nospam.invalid> - 2011-04-15 00:47 -0700
        Re: Pythonic infinite for loop? Peter Otten <__peter__@web.de> - 2011-04-15 10:11 +0200
  Re: Pythonic infinite for loop? Peter Otten <__peter__@web.de> - 2011-04-15 10:25 +0200
    Re: Pythonic infinite for loop? Chris Angelico <rosuav@gmail.com> - 2011-04-15 18:32 +1000
      Re: Pythonic infinite for loop? Peter Otten <__peter__@web.de> - 2011-04-15 11:32 +0200
      Re: Pythonic infinite for loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-04-15 12:48 +0000

csiph-web