Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76590
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.com> |
| 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; 'python.': 0.02; 'else:': 0.03; 'exercise': 0.04; 'aggregate': 0.07; 'attributes': 0.09; 'fname': 0.09; 'integers': 0.09; 'omit': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'kurt': 0.12; '-tkc': 0.16; '365': 0.16; 'averaging': 0.16; 'collections': 0.16; 'columns': 0.16; 'commas,': 0.16; 'csv': 0.16; 'defaultdict': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'files.': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'headers': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'appear': 0.29; 'leave': 0.29; 'subject:list': 0.30; '"do': 0.31; 'file:': 0.31; 'omitted': 0.31; 'file': 0.32; 'lists': 0.32; 'plain': 0.33; 'something': 0.35; 'charset:us-ascii': 0.36; 'so,': 0.37; 'files': 0.38; 'does': 0.39; 'how': 0.40; 'days': 0.60; "you're": 0.61; 'times': 0.62; 'offer': 0.62; 'skip:n 10': 0.64; 'total': 0.65; 'to:addr:gmail.com': 0.65; 'details': 0.65; 'details,': 0.68; 'repeat': 0.74; '*and*': 0.84; 'avg': 0.84; 'received:50.22': 0.84; 'average': 0.93 |
| Date | Tue, 19 Aug 2014 12:58:09 -0500 |
| From | Tim Chase <python.list@tim.thechases.com> |
| To | Kurt <kplunt@gmail.com> |
| Subject | Re: Functions on list items |
| In-Reply-To | <33c7dfd2-015a-4b3f-a3c8-7bf4d6b6cb73@googlegroups.com> |
| References | <33c7dfd2-015a-4b3f-a3c8-7bf4d6b6cb73@googlegroups.com> |
| X-Mailer | Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-AntiAbuse | This header was added to track abuse, please include it with any abuse report |
| X-AntiAbuse | Primary Hostname - boston.accountservergroup.com |
| X-AntiAbuse | Original Domain - python.org |
| X-AntiAbuse | Originator/Caller UID/GID - [47 12] / [47 12] |
| X-AntiAbuse | Sender Address Domain - tim.thechases.com |
| X-Get-Message-Sender-Via | boston.accountservergroup.com: authenticated_id: tim@thechases.com |
| Cc | python-list@python.org |
| 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.13158.1408471182.18130.python-list@python.org> (permalink) |
| Lines | 65 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1408471182 news.xs4all.nl 2856 [2001:888:2000:d::a6]:51149 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:76590 |
Show key headers only | View raw
On 2014-08-19 10:34, Kurt wrote:
> I am trying to process the following calendar and data attributes
> in a file: Da Mo Yr AttrA AttrB AttrC...
> I need to average AttrA for each of 365 Da days across Yr years.
> Then do the same for 27K files. Repeat for AttrB, AttrC etc. Can I
> do the averaging with lists or do I need some Python Db module (if
> it exists)?
Unless these files are huge *AND* you plan to perform this operation
repeatedly (rather than once), just do it in plain Python. (though
Python does offer both the "anydbm" and "sqlite3" modules for
database'ish stuff)
You omit some key details, so the example below would need to be
tweaked depending on things like:
- does it have headers
- how are the columns separated? (tabs, commas, spaces?)
- can a single date appear multiple times in a single file? (and if
so, what would you intend to do with it?)
- do you need aggregate averages across all the files, or just
per-file?
- are those values you're averaging integers or floats?
It would look something like (untested)
import csv
from collections import defaultdict
def report(fname, names_avgs):
"Do something with the averages"
print(fname)
for name, avg in names_avgs:
print(" %s: %f" % (name, avg))
def no_data(fname):
"Do something if a file has no data"
pass
for fname in fname_iter():
sums = defaultdict(float)
row_count = 0
with open(fname) as f:
r = csv.DictReader(f):
for row in r:
row_count += 1
for data in ("AttrA", "AttrB", "AttrC"):
sums[data] += float(row[data])
if row_count:
report(fname, [
(fieldname, total/row_count)
for fieldname, total
in sums
])
else:
no_data(fname)
I leave the details of fname_iter() and tweaking it based on the
omitted details as an exercise to the reader. :-)
-tkc
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Functions on list items Kurt <kplunt@gmail.com> - 2014-08-19 10:34 -0700
Re: Functions on list items Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-19 18:43 +0100
Re: Functions on list items Kurt <kplunt@gmail.com> - 2014-08-19 10:56 -0700
Re: Functions on list items Tim Chase <python.list@tim.thechases.com> - 2014-08-19 12:58 -0500
csiph-web