Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #3259
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Pythonic infinite for loop? |
| Followup-To | comp.lang.python |
| Date | 2011-04-15 11:32 +0200 |
| Organization | None |
| 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> |
Followups directed to: comp.lang.python
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 | Next — Previous in thread | Next in thread | Find similar
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